DEV Community

Kaleem for This is Learning

Posted on

How to easily migrate Heroku Postgres database between apps.

There are two ways to copy data between databases: restore backup or copy command.

We will be performing a database migration from the myapp-prod app to the myapp-test app that is copying the schema (structure) and data from the source to target.

Restore backup

Copies specified backup version of the database.

heroku pg:backups:capture --app myapp-prod

Creates a backup in the app myapp-prod. The Heroku console will tell you backup ID version b001 in this case.

heroku pg:backups:restore myapp-prod::b001 DATABASE_URL --app myapp-test

Second command: Copy from myapp-prod app backup id b001 (latest backup) to default DATABASE_URL of the app myapp-test.

Direct copy

Copies the latest state of the database.

heroku pg:copy myapp-prod::DATABASE_URL DATABASE_URL --app myapp-test

Copy from myapp-prod app latest default database DATABASE_URL copy to app myapp-test default database DATABASE_URL.

Here DATABASE_URL is the name of the default database of your Heroku apps.

Thanks :)

Reference
https://devcenter.heroku.com/articles/heroku-postgres-backups#restore-a-backup

Top comments (0)