September 5, 2018

Restoring a database from Heroku for local development

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. Not it’s time to create a database clone out of that dump file.

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.


Next post
Django templates: 'include' context Something I learned today which should come handy. The tag allows rendering a partial template from another: So I was doing this to pass context to