Skip to content

Instantly share code, notes, and snippets.

@laranea
Forked from hartleybrody/import-db.sh
Created February 21, 2019 13:01
Show Gist options
  • Select an option

  • Save laranea/ec862955277a44f7ca28452ec72f413e to your computer and use it in GitHub Desktop.

Select an option

Save laranea/ec862955277a44f7ca28452ec72f413e 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
export DB_NAME=$(echo $DATABASE_URL | sed 's:.*/::')
pg_restore --verbose --clean --no-acl --no-owner -h localhost -d $DB_NAME 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 | grep DATABASE_URL | sed 's/DATABASE_URL: //g') < dump.sql
# remove the dump
rm dump.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment