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
First create an up to date backup of production:
heroku pgbackups:capture -a productionappslug --expire
Find out what colour Heroku has named your database.
https://postgres.heroku.com/databases or https://dashboard.heroku.com/apps/STAGINGAPPSLUG/resources
Then load the production db backup into staging (changing RED to whatever colour yours is):
heroku pgbackups:restore HEROKU_POSTGRESQL_RED -a stagingappslug `heroku pgbackups:url -a productionappslug`
stagingappslug and liveappslug are whatever shortnames your heroku apps are called.
UPDATE:
You can run this command to transfer the database from production to staging:
heroku pg:copy your-app::DATABASE_URL DATABASE_URL -a yourapp-staging
It will prompt you to confirm the action, and once you have done that, all of the data will be migrated.
To transfer (copy) the production database (source database) to the staging database (target database) you will need to invoke pg:copy
from the target application, referencing a source database.
heroku pg:copy source-application::OLIVE HEROKU_POSTGRESQL_PINK -a target-application
Another example:
heroku pg:copy my-production-app::HEROKU_POSTGRESQL_OLIVE HEROKU_POSTGRESQL_PINK --app my-staging-app
To obtain the colour names of your databases, use:
heroku pg --app my-production-app
heroku pg --app my-staging-app
See pg:copy
After NO LUCK. (Im using heroku gem 2.31.4) I did the following (help for the weary)
Login into Heroku Database console
Login to staging > 'settings' > PGRestore > Copy 'Connection Settings' into a text file.
Login to production > Snapshots, press '+' to make a new backup as of now. Then press download. Download into the apps /tmp folder or whever you want.
Set staging to maintenance mode
$ heroku maintenance:on
Run the command like so, with Connection Settings text and dump file at the end: PGPASSWORD={...bits of stuff here...} -p 5432 'tmp/b048.dump.dump'
After run:
$ heroku maintenance:off
Login to staging and check that things match. Find a recent transaction you know is in production if you can via. $ heroku run console for both apps and check ids match.
This works for me:
heroku pg:copy you-app-production::DATABASE DATABASE -a you-app-staging
Update for mid-2017 (stealing from Takehiro Mouri's answer - simplify the DATABSE_NAME part)
Update for mid-2015...
The pgbackups add-on has been deprecated. No more pgbackups:transfer
.
To copy a database from yourapp to yourapp_staging:
# turn off the web dynos in staging
heroku maintenance:on -a yourapp-staging
# if you have non-web-dynos, do them too
heroku ps:scale worker=0 -a yourapp-staging
# backup the staging database if you are paranoid like me (optional)
heroku pg:backups capture -a yourapp-staging
# execute the copy
heroku pg:copy your-app::DATABASE_URL DATABASE_URL -a yourapp-staging
Then when it's complete, turn staging back on:
# this is if you have workers, change '1' to whatever
heroku ps:scale worker=1 -a yourapp-staging
heroku maintenance:off -a yourapp-staging
(source: https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#upgrade-with-pg-copy-default)