Sync production database to the staging database

Jescart

Jescart / December 28, 2018

There are two steps in Heroku in syncing production database to staging database.


STEP 1: Create a duplicate of production database into your local.

`pg:pull` can be used to pull remote data from a Heroku Postgres database to a database on your local machine. The command looks like this:

heroku pg:pull DATABASE_URL [local-database] --app [app-name-of-your-prod-database]

This command creates a new local database named `[local-database]` and then pulls data from the database at DATABASE_URL from the `[app-name-of-your-prod-database]`.

Note: To prevent accidental data overwrites and loss, the `[local-database]` must not already exist. You will be prompted to drop an already existing local database before proceeding.


STEP 2: Sync the duplicated production database to the staging database.

`pg:push` pushes data from a local database into a remote Heroku Postgres database. The command looks like this:

heroku pg:push [local-database] DATABASE_URL --app [app-name-of-your-staging-database]

This command takes the local database `[local-database]` and pushes it to the database at DATABASE_URL on the `[app-name-of-your-staging-database]`.

Note: To prevent accidental data overwrites and loss, the remote database must be empty. You will be prompted to `pg:reset` a remote database that is not empty.