Last Updated: January 28, 2019
·
4.995K
· ahacop

Copy postgres databases between Heroku apps.

Say we have a lot of data on our production database and we'd like to replace our staging database with that data. Well, this is how we can do it:

First make a backup of your production database:

heroku pg:backups capture --app PRODUCTION

Then restore the backup (which is on S3) right into your staging database:

heroku pg:backups restore heroku pg:backups public-url --app PRODUCTION DATABASE --app STAGING

Where DATABASE represents the HEROKU_POSTGRESQL_COLOR_URL of the database you wish to restore to. You may also specify the default application database using DATABASE_URL.

3 Responses
Add your response

Hi ! Thanks for the article, it helped me a lot through this jungle of commands.
Those commands have evolved a bit since your article was written:
$ heroku pg:backups restore heroku pg:backups public-url --app PRODUCTION DATABASE --app STAGING

over 1 year ago ·

Babgyy, thanks for contributing the new commands! I've updated the pro-tip accordingly.

over 1 year ago ·

Hi! Thanks for the tip, it was very helpful. But I've found a problem with it, the second Heroku call needs to be wrapped in backticks or $(). So would be something like this:

$ heroku pg:backups restore $(heroku pg:backups public-url --app PRODUCTION) --app STAGING

Best regards.

over 1 year ago ·