Restoring a database from Heroku for local development

September 5, 2018

I’ve recently had to download my Django app’s database for local inspection. Heroku lets you do that pretty easily with:

$ heroku pg:backups:download

This gets you a .dump file. Now it’s time to create a database clone out of it.

Here’s the gist. We first create a new database:

$ sudo -u USERNAME createdb NEW_DATABASE_NAME

Note that USERNAME and NEW_DATABASE_NAME should be replaced with the respective values.

The next step to is to restore the downloaded .dump to the database we just created:

$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -d NEW_DATABASE_NAME /PATH/TO/latest.dump

And now there’s a database clone that you can connect to at NEW_DATABASE_NAME. It’s also possible to overwrite an existing database by supplying its name instead of a new database name, which makes the database-creation step redundant.

The process usually finishes with some reported errors, but I never noticed anything weird or wrong with database copies I’ve generated this way.