Skip to content

Instantly share code, notes, and snippets.

@adamlogic
Last active January 17, 2018 15:13
Show Gist options
  • Save adamlogic/37faf41cbfc97cb20e58d828632c2d80 to your computer and use it in GitHub Desktop.
Save adamlogic/37faf41cbfc97cb20e58d828632c2d80 to your computer and use it in GitHub Desktop.
Wrapper script around `heroku pg:pull` for loading a local database
#!/bin/bash
# Example:
# pgpull -r production
#
# Options are passed through to `heroku pg:pull`, so this would look for a git branch
# named "production".
opts=$*
# Replace this with your local database name
local_db=my_app_development
remote_db=$(heroku pg:info $opts | grep 'Add-on' | cut -d: -f2 | tr -d '[:space:]')
echo "--- killing any connections to the local DB so we can drop it"
psql -d $local_db -c "select pg_terminate_backend(pid) from pg_stat_activity where datname='$local_db';" > /dev/null 2>&1
echo "--- dropping local DB"
dropdb $local_db --if-exists
echo "--- pulling remote DB"
heroku pg:pull $remote_db $local_db $opts
echo "--- wiping sensitive data"
# This is a placeholder. Wipe out any data that's personally identifying or could result in impacting real users.
psql -d $local_db -c "update users set email = 'REDACTED';" > /dev/null 2>&1
psql -d $local_db -c "update apps set slack_webhook_url = 'REDACTED';" > /dev/null 2>&1
@surfacedamage
Copy link

@adamlogic, I had to change line 12 to cut field 3, not 2 in order for it to grab the correct database name. I am running heroku-cli/6.15.13-3dce47c if it matters.

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