Skip to content

Instantly share code, notes, and snippets.

@hartleybrody
Last active May 7, 2025 14:43
Show Gist options
  • Save hartleybrody/44a5e54a477dbfa6c091de0e6ea50302 to your computer and use it in GitHub Desktop.
Save hartleybrody/44a5e54a477dbfa6c091de0e6ea50302 to your computer and use it in GitHub Desktop.
Copy data from Heroku Postgres into local database
# copy/import data from heroku postgres to localhost pg database
# useful for copying admin's work on live site into local database to reproduce errors
# https://devcenter.heroku.com/articles/heroku-postgres-import-export
# take heroku pg snapshot and download
heroku pg:backups:capture
heroku pg:backups:download
# load the dump into local postgres database, assuming $DATABASE_URL set locally
pg_restore --verbose --clean --no-acl --no-owner -d $DATABASE_URL latest.dump
rm latest.dump
# =============================================================================== #
# =============================================================================== #
# =============================================================================== #
# copy/import from local database into heroku DB
# dump your local database into a sql file
pg_dump $DATABASE_URL > dump.sql
# import it into the heroku database
psql $(heroku config:get DATABASE_URL) < dump.sql
# remove the dump
rm dump.sql
# =============================================================================== #
# =============================================================================== #
# =============================================================================== #
# if you get a server version mismatch, like this
> pg_dump $DATABASE_URL > dump.sql
pg_dump: error: server version: 16.6; pg_dump version: 12.22 (Ubuntu 12.22-0ubuntu0.20.04.2)
pg_dump: error: aborting because of server version mismatch
# you can run pg_dump with the correct "server" version using docker
docker run postgres:{SERVER_VERSION_FROM_ERROR_MESSAGE} pg_dump $DATABASE_URL > dump.sql
# for example:
docker run postgres:16.6 pg_dump $DATABASE_URL > dump.sql
via https://stackoverflow.com/a/39926357/625840
@hartleybrody
Copy link
Author

instructions for sqlite, via: https://alexwlchan.net/2025/copying-sqlite-databases/

dump the database to a text file (often much smaller than the actual DB, since it simply describes the indexes, instead of containing all of the data for the indexes:

sqlite3 my_database.db .dump > my_database.db.txt


reconstruct the DB locally

cat my_database.db.txt | sqlite3 my_reconstructed_database.db

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