Skip to content

Instantly share code, notes, and snippets.

@pije76
Forked from stevenyap/Heroku Database.md
Created May 21, 2018 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pije76/cf62f5d3d23e68106a79fb715718512a to your computer and use it in GitHub Desktop.
Save pije76/cf62f5d3d23e68106a79fb715718512a to your computer and use it in GitHub Desktop.
Heroku for backup, exporting and importing database

Note: You need to have to Heroku PGBackup addon first heroku addons:add pgbackups..

Importing Heroku PG DB to local

Creates the backup on heroku first:

heroku pgbackups:capture

Download it to local via curl:

curl -o latest.dump `heroku pgbackups:url`

Restore it:

pg_restore --verbose --clean --no-acl --no-owner -h localhost -d mydbname latest.dump

Remove the dump:

rm latest.dump

Exporting local to Heroku

Create your DB dump locally:

pg_dump -Fc --no-acl --no-owner -h localhost mydbname > mydb.dump

**Add PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH" in .bash_profile if you encountered version does not match error when doing pg_dump **

Upload mydb.dump to your S3 or a remote FTP.
Note: remember to set permission for file uploaded to S3

Next, call Heroku to download the dump and then restore it:

heroku pgbackups:restore DATABASE 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump'

Delete a Postgres Backup

heroku pgbackups # To see a list of all backups
heroku pgbackups:destroy <BACKUP_ID>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment