Importing and Exporting Heroku Postgres Databases
Last updated December 01, 2022
On the surface, PG Backups 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’s also a useful tool capable of exporting to or importing from external PostgreSQL databases.
PG Backups uses the native
pg_dump PostgreSQL tool to create its backup files, making it trivial to export to other PostgreSQL installations. The resulting backup file uses the custom format option in
pg_dump. As compared to the plain-text format, the custom format options result in backup files that can be much smaller.
In general, PGBackups are intended for moderately loaded databases up to 20 GB. Contention for the I/O, memory, and CPU needed for backing up a larger database becomes prohibitive at a moderate load and the longer run time increases the chance of an error that ends your backup capture prematurely. For databases that are larger than 20 GB, see Capturing Logical Backups on Larger Databases.
To export the data from your Heroku Postgres database, create a new backup and download it.
$ heroku pg:backups:capture --app example-app $ heroku pg:backups:download --app example-app
Restore to local database
Load the dump into your local database using the pg_restore tool. If objects exist in a local copy of the database already, you can run into inconsistencies when doing a
This process usually generates some warnings, due to differences between your Heroku database and a local database, but they’re generally safe to ignore.
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump
If you’re using an old version of
pg_restore, you can see an error such as
pg_restore: [archiver] unsupported version (1.13) in file header when you try to run
pg_restore. Ensure that the
pg_restore version you’re using is up-to-date and compatible with the version of the exported database. You can check your version of
pg_restore by running
You can optionally use the
--jobs <number of jobs> flag with
pg_restore to parallelise the restore of the dump. Only the custom and directory archive formats are supported with this option. More on this option can be found in the Postgres documentation.
PG Backups can be used as a convenient tool to import database dumps from other sources into your Heroku Postgres database.
If you’re importing data as part of the initialization of a new application, you must 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:
# set the password in an environment variable export PGPASSWORD=mypassword # linux/mac set PGPASSWORD=mypassword # windows # create the database dump $ pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
Import to Heroku Postgres
pg:backups restore command drops any tables and other database objects before recreating them.
Generate a signed URL using the aws console:
$ aws s3 presign s3://your-bucket-address/your-object
Use the raw file URL in the
pg:backups restore command:
$ heroku pg:backups:restore '<SIGNED URL>' DATABASE_URL --app example-app
DATABASE_URL represents the
HEROKU_POSTGRESQL_COLOR_URL of the database you wish to restore to. You must specify a database configuration variable to restore the database.
If you’re using a Unix-like operating system, make sure to use single quotes around the temporary S3 URL, because it can contain ampersands and other characters that confuse your shell. If you’re running Windows, you must use double-quotes.
When you’ve completed the import process, delete the dump file from its storage location if it’s no longer needed.