Preparation
Workspace
The amount of time it takes to dump and restore a Database is directly related to the size of the Database and network bandwidth. If the Database being dumped is small (< 10 GB) and bandwidth is decent, then dumping locally is usually fine. Otherwise, consider dumping and restoring from a server with more bandwidth, such as an AWS EC2 Instance. Another thing to consider is available disk space. There should be at least as much space locally available as the Database is currently taking up on disk. See the Database’s metrics to determine the current amount of space it’s taking up. If there isn’t enough space locally, this would be another good indicator to dump and restore from a server with a large enough disk. All of the following instructions should be completed on the selected machine.Test the schema
If data is being transferred to a Database running a different PostgreSQL version than the original, first check that the schema can be restored on the desired version by following the How to test a PostgreSQL Database’s schema on a new version guide. If the same PostgreSQL version is being used, this is not necessary.Test the upgrade
Testing the schema should catch most issues but it’s also recommended to test the upgrade before performing it in production. The easiest way to do this is to restore the latest backup of the Database and performing the upgrade against the restored Database. The restored Database should have the same container size as the production Database. Example:pg_dumpall
and psql
client tools.
Configuration
Collect information on the Database you’d like to upgrade and store it in the following environment variables for use later in the guide:-
SOURCE_HANDLE
- The handle (i.e. name) of the Database. -
SOURCE_ENVIRONMENT
- The handle of the environment the Database belongs to.
-
TARGET_HANDLE
- The handle (i.e. name) for the Database. -
TARGET_VERSION
- The target PostgreSQL version. Runaptible db:versions
to see a full list of options. -
TARGET_ENVIRONMENT
- The handle of the environment to create the Database in. -
TARGET_DISK_SIZE
- The size of the target Database’s disk in GB. This must be at least be as large as the current Database takes up on disk but can be smaller than its overall disk size. -
TARGET_CONTAINER_SIZE
(Optional) - The size of the target Database’s container in MB. Having more memory and CPU available speeds up the dump and restore process, up to a certain point. See the Database Scaling documentation for a full list of supported container sizes.
Create the target Database
Create a new Database running the desired version. Assuming the environment variables above are set, this command can be copied and pasted as-is to create the Database.Execution
Scale Services down
Scale all Services that use the Database down to zero containers. It’s usually easiest to prepare a script that scales all Services down and another that scales them back up to their current values once the upgrade has been complete. Current container counts can be found in the Aptible Dashboard or by runningAPTIBLE_OUTPUT_FORMAT=json aptible apps
.
Example scale command:
Dump the data
In a separate terminal, create a Database Tunnel to the source Database using the Aptible CLI.aptible db:tunnel
, and store it in the following environment variables in the original terminal:
-
SOURCE_URL
- The full URL of the Database tunnel. -
SOURCE_PASSWORD
- The Database’s password.
dump.sql
in this case.
pg_dumpall
is piped into grep
in order to remove any SQL commands that may change the default aptible
user’s password. If these commands were to run on the target Database, it would be updated to match the source Database. This would result in the target Database’s password no longer matching what’s displayed in the Aptible Dashboard or printed by commands like aptible db:url
or aptible db:tunnel
which could cause problems down the road.
You now have a copy of your Database’s schema and data in dump.sql
! The Database Tunnel can be closed by following the instructions that aptible db:tunnel
printed when the tunnel started.
Restore the data
In a separate terminal, create a Database Tunnel to the target Database using the Aptible CLI.aptible db:tunnel
, and store it in the TARGET_URL
environment variable in the original terminal.
Example:
psql
can be noisy depending on the size of the source Database. In order to reduce the noise, the output is redirected to /dev/null
so that only error messages are displayed.
The following errors may come up when restoring the Database:
Errors
If there are additional errors, they will need to be addressed in order to be able to upgrade the source Database to the desired version. Consult the PostgreSQL Documentation for details about the errors you encounter. Once you’ve updated the source Database, you can try the dump again by deprovisioning the target Database and starting from the Create the target Database step.$TARGET_ENVIRONMENT
is configured to retain final Database Backups, which is enabled by default, you may want to delete the final backup for the target Database.
You can obtain a list of final backups by running:
aptible backup:purge
command.
Update Services
Once the upgrade is complete, any Services that use the existing Database need to be updated to use the upgraded target Database. Assuming you’re supplying the Database Credentials through the App’s Configuration, this can usually be easily done with theaptible config:set
command.
Example config command: