Preparation
Step 0: Install the necessary tools
Install the Aptible CLI and PostgreSQL Client Tools. This guide uses thepg_dumpall
and psql
client tools.
Step 1: Configuration
Collect information on the Database you’d like to test 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 theDatabase 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.
Step 2: 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.aptible db:create
creates a Database with a 1 GB of memory and 10 GB of disk space. This should be sufficient for most schema tests but, if more memory or disk is required, the --container-size
and --disk-size
arguments can be used.
Execution
Step 1: Dump the schema
Create a Database Tunnel to the source Database using the Aptible CLI.aptible db:tunnel
, and store it in the following environment variables:
-
SOURCE_URL
- The full URL of the Database tunnel. -
SOURCE_PASSWORD
- The Database’s password.
schema.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 in schema.sql
! The Database Tunnel can be closed by following the instructions that aptible db:tunnel
printed when the tunnel started.
Step 2: Restore the schema
Create a Database Tunnel to the target Database using the Aptible CLI.aptible db:tunnel
, in the TARGET_URL
environment variable.
Example:
psql
can be noisy depending on the complexity of the source Database’s schema. 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 schema:
Cleanup
Step 1: Deprovision the target Database
Step 2: Delete Final Backups (Optional)
If the$TARGET_ENVIRONMENT
is configured to retain final Database Backups, which is enabled by default, you may want to delete the final backups for all target Databases you created for this test.
You can obtain a list of final backups by running:
aptible backup:purge
command.