Skip to content

Instantly share code, notes, and snippets.

@gilbert
Created December 16, 2014 20:13
Show Gist options
  • Save gilbert/5f74a51f7392f53658f0 to your computer and use it in GitHub Desktop.
Save gilbert/5f74a51f7392f53658f0 to your computer and use it in GitHub Desktop.
Pulling a Heroku Postgres DB

Pulling a Heroku Postgres DB

Add the following to your .bash_profile / .profile / whatever:

function heroku_pg_pull(){
  echo "!   WARNING: Data in the local database '$2' will be destroyed."
  echo "    Type '$2' to overwrite data in local database '$2'"
  read -p "> " local_database_name
  echo
  if [ "$local_database_name" == "$2" ]; then
    curl -o heroku_pg_pull_latest_backup.dump `heroku pgbackups:url -a $1`;
    pg_restore --verbose --clean --no-acl --no-owner -h localhost -U `whoami` -d $2 heroku_pg_pull_latest_backup.dump;
    rm heroku_pg_pull_latest_backup.dump;
  else
    echo "Aborted"
  fi
}

You only need to do this once.

After you have that available, run the following commands to pull from a remote heroku database:

$ heroku pgbackups:capture --expire -a my_heroku_app_name
$ heroku_pg_pull my_heroku_app_name my_local_db_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment