How do I transfer production database to staging on Heroku using pgbackups? Getting error

后端 未结 12 2164
刺人心
刺人心 2021-01-30 03:13

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

12条回答
  •  隐瞒了意图╮
    2021-01-30 03:42

    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
    

提交回复
热议问题