Table of Contents
On the surface, PG Backups add-on provides a way to capture regular backups of your Heroku Postgres database. However, because of its general-purpose architecture and use of standard PostgreSQL utilities, it is also a useful tool capable of exporting and importing to/from external PostgreSQL databases.
Both use-cases assume you have the PG Backups add-on provisioned for the applicable application.
PG Backups uses the native
pg_dump PostgreSQL tool to create its backup files, making it trivial to export to other PostgreSQL installations.
To export the data from your Heroku Postgres database to a local PostgreSQL database, create a new backup and use any number of download tools, such as
wget, to store the backup locally.
$ heroku pgbackups:capture $ curl -o latest.dump `heroku pgbackups:url`
Restore to local database
Load the dump into your local database using the pg_restore tool.
This will usually generate some warnings, due to differences between your Heroku database and a local database, but they are generally safe to ignore.
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump
PG Backups can be used as a convenient tool to import database dumps from other sources to your Heroku Postgres database.
If you are importing data as part of the initialization of a new application you will need to first create and configure the app on Heroku before performing the import.
Create dump file
Dump your local database in compressed format using the open source pg_dump tool:
$ PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
Import to Heroku Postgres
In order for PG Backups to access and import your dump file you will need to upload it somewhere with an HTTP-accessible URL. We recommend using Amazon S3
Use the raw file URL in the
Be sure to use single quotes around the temporary S3 URL, as it contains ampersands and other characters that will confuse your shell otherwise.
$ heroku pgbackups:restore DATABASE 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump'
Note that the pgbackups:restore command drops any tables and other database objects before recreating them.
DATABASE represents the
HEROKU_POSTGRESQL_COLOR_URL of the database you wish to restore to. If no DATABASE is specified, it defaults to the applications current
On completion of the import process be sure to delete the dump file from its storage location if it’s no longer needed.