Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Last active November 24, 2022 15:29
Show Gist options
  • Save wrburgess/5528649 to your computer and use it in GitHub Desktop.
Save wrburgess/5528649 to your computer and use it in GitHub Desktop.
Backup Heroku Postgres database and restore to local database

Grab new backup of database

Command: heroku pgbackups:capture --remote production

Response: >>> HEROKU_POSTGRESQL_COLOR_URL (DATABASE_URL) ----backup---> a712

Get url of backup download

Command: heroku pgbackups:url [db_key] --remote production

Example: heroku pgbackups:url a712 --remote production

>>> "https://s3.amazonaws.com/hkpgbackups/app11111118@heroku.com/a712.dump?AWSAccessKeyId=XXXXXXXXXXXXXX&Expires=1367876175&Signature=XXXXXXX%EEEEEEEYWWWWWWW%3D"

Download backup dump file

Command: curl "http://[url]" > production.dump

Example: curl "https://s3.amazonaws.com/hkpgbackups/app11111118@heroku.com/a712.dump?AWSAccessKeyId=XXXXXXXXXXXXXX&Expires=1367876175&Signature=XXXXXXX%EEEEEEEYWWWWWWW%3D" > production.dump

Restore backup dump into local db

Command: pg_restore --verbose --clean --no-acl --no-owner -h localhost -d [db_name] production.dump

@roostercrab
Copy link

Heroku's instructions for anything are terrible, thank you to @SimMorad for finally giving me the commands that I have been digging for to quickly pull all the data from the Heroku's database, delete the database, rerun the migrations (for django but I imagine it's similar for ruby), then repopulate the data into Heroku's database.

For the question about where to get the database username and password, Heroku makes them up and changes them constantly for security so you have to get the most current ones each time. You do that by going to your app, then clicking on your Heroku Postgres link which should take you to https://data.heroku.com/datastores/ which will have a bunch of numbers specific to your database. From there, click on Settings and then View Credentials... and populate the command with that information. It will ask you for your password after that. Here's the command from @SidMorad with the variables highlighted. I used it for django, the only difference for other frameworks would be in the migration command I imagine:

(-r development is because I have a pipeline for multiple apps, you can take it off if you only have one; or change it to what you called yours in heroku git)

WAY TO BACKUP, DELETE, MIGRATE (DJANGO) AND THEN RESTORE ORIGINAL POSTGRES DB DATA FOR HEROKU

THIS WILL DELETE YOUR DATA BEFORE YOU RESTORE IT SO PRACTICE ON SOMETHING INCONSEQUENTIAL BEFORE DOING ON SOMETHING THAT YOU CAN'T AFFORD TO LOSE DATA ON!!!

heroku pg:backups:capture -r development
heroku pg:backups:download -r development

heroku pg:reset -r development

heroku run python manage.py migrate -r development

*** need to get credentials from heroku db and fill in the asterisked parts, names match what they are called in heroku ***
*** https://data.heroku.com/datastores/ <--accessible from your app, Heroku Postgres then go to Settings, View Credentials...***
pg_restore --verbose --clean --no-acl --no-owner -h Host -U User -d Database latest.dump

It will look like this (my actual numbers were changed before posting, but it should look like random nonsense like this)
pg_restore --verbose --clean --no-acl --no-owner -h ec2-34-267-351-999.compute-3.amazonaws.com -U nvjrwueedkssdhd -d f88gu444c2vwdt latest.dump

Your command line will then ask you for the Password from heroku which is an extremely long series of characters from the same page that looks like this (again these are just random characters):
4f69ba471fdb64a9b11a3b739bc83382952a6284c20333a4ad8893bcf221a24dc

I have a theory that Heroku has made things difficult to learn on accident, and then people feel dumb because they can't figure it out. If you're in that position just know that you're not alone, it has been a year of suffering before I even started to get it to work in a reasonable fashion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment