On Heroku, I am trying to copy the production database into my staging app using the pgbackups addon. I followed the instructions on the addon page: https://devcenter.heroku.com
Here's a simple and safe solution to this using Heroku's add-on attachments and forking. It does not require backups, no downtime, and does not overwrite any database.
You need to attach the production database to the staging app first, then fork on the staging app. If you fork on the production app, then attach to the staging app, the billing app will be the production app and you won't be able to detach the fork from it.
1. First find out the add-on name of your production database (here it is postgres-prod-123
):
$ heroku addons --app myapp-production
heroku-postgresql (postgresql-prod-123) standard-0 $50/month
└─ as DATABASE
2. Then attach the production database add-on to your staging app. Give it a name like PRODUCTION_DB
to make it easy to recognize:
$ heroku addons:attach postgresql-prod-123 --app myapp-staging --as PRODUCTION_DB
3. Then create a fork of the production database on the staging app:
$ heroku addons:create heroku-postgresql:standard-0 --fork PRODUCTION_DB_URL --as STAGING_DB --app myapp-staging
4. Finally promote the fork to be the primary database of your staging app:
$ heroku pg:promote STAGING_DB --app myapp-staging
Done! Your staging app is now using a copy of your production database. Note that your previous staging database is still there, you may want to destroy it after you've made sure everything works.
To clean up, detach the production database from the staging app:
$ heroku addons:detach postgresql-prod-123 --app myapp-staging