DEV Community

Cover image for How to copy Heroku production database into development environment?
Yaroslav Shmarov
Yaroslav Shmarov

Posted on • Originally published at blog.corsego.com on

How to copy Heroku production database into development environment?

load Heroku production database in development

Sometimes you want to run a copy of your production database in development.

It can be really useful for debugging, or experimenting with potentially dangerous operations.

Here’s how you can run your Heroku Postgresql database in development:

  • 1 Create Heroku backup
heroku pg:backups:capture --app myappname
Enter fullscreen mode Exit fullscreen mode
heroku pg:backups:download --app myappname
Enter fullscreen mode Exit fullscreen mode
  • 3 Reset local database
rails db:drop
rails db:drop DISABLE_DATABASE_ENVIRONMENT_CHECK=1
rails db:create
Enter fullscreen mode Exit fullscreen mode
  • 4 Populate local database from heroku download

(place development database name from database.yml)

pg_restore -h localhost -d myappname_development latest.dump
Enter fullscreen mode Exit fullscreen mode
  • 5 run migrations
rails db:migrate
Enter fullscreen mode Exit fullscreen mode
  • 6 Remove downloaded database from app repository:
rm latest.dump 
Enter fullscreen mode Exit fullscreen mode

That’s it! Now you have a copy of your production database running in development🥳!


Troubleshooting

createuser --interactive --pwprompt
yaro
pass
Enter fullscreen mode Exit fullscreen mode
  • Specify newly created postgresql user for restoring database and restore
pg_restore -h localhost -U yaro -d myappname_development latest.dump
pass
Enter fullscreen mode Exit fullscreen mode

or

PGPASSWORD=pass pg_restore -h localhost -U yaro -d myappname_development latest.dump
Enter fullscreen mode Exit fullscreen mode

or

set "PGPASSWORD=pass"
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U yaro -d myappname_development latest.dump
Enter fullscreen mode Exit fullscreen mode

Adding latest.dump to gitignore:

echo 'latest.dump*' >> .gitignore
Enter fullscreen mode Exit fullscreen mode

Discussion (0)