Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Created October 18, 2013 13:52
Show Gist options
  • Save stevenyap/7041858 to your computer and use it in GitHub Desktop.
Save stevenyap/7041858 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>
@huxaiphaer
Copy link

Am Exporting local to Heroku , but am using windows am encountering this error

PGPASSWORD=namungoona : The term 'PGPASSWORD=namungoona' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ PGPASSWORD=namungoona  pg_dump -Fc --no-acl --no-owner -h localhost - ...
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (PGPASSWORD=namungoona:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

what should i do?

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