Skip to content

Instantly share code, notes, and snippets.

@edw
Last active May 6, 2022 17:33
Show Gist options
  • Save edw/5237669 to your computer and use it in GitHub Desktop.
Save edw/5237669 to your computer and use it in GitHub Desktop.
Dump a schema from a Heroku app's database.
#!/bin/sh
# Usage: heroku-pg-dump-schema.sh HEROKU-APP-NAME DB-ENV-VAR-NAME SCHEMA-NAME
app=$1
env_var=$2
schema=$3
db_url=`heroku config -s --app ${app} | grep ${env_var}`
export PGDATABASE=`sed -E 's|^.*/([a-zA-Z0-9]*)$|\1|' <<EOF
$db_url
EOF`
export PGHOST=`sed -E 's|^.*@([-.a-zA-Z0-9]*):.*$|\1|' <<EOF
$db_url
EOF`
export PGPORT=`sed -E 's|^.*:([-.a-zA-Z0-9]*)/.*$|\1|' <<EOF
$db_url
EOF`
export PGUSER=`sed -E 's|^.*//([a-zA-Z0-9]*):.*$|\1|' <<EOF
$db_url
EOF`
export PGPASSWORD=`sed -E 's|^.*:([a-zA-Z0-9]*)@.*$|\1|' <<EOF
$db_url
EOF`
pg_dump --schema ${schema} -Osx
@weilu
Copy link

weilu commented Apr 13, 2022

Original script required modification for it to work (for me anyway). Modified script below in case it might help someone else

#!/bin/sh# Usage: heroku-pg-dump-schema.sh HEROKU-APP-NAME SCHEMA-NAME
​
app=$1
schema=$2
​
db_url=`heroku config --app ${app} | grep HEROKU_POSTGRESQL`export PGDATABASE=`sed -E 's/^.*\/([a-zA-Z0-9]*)$/\1/' <<EOF
$db_url
EOF`
export PGHOST=`sed -E 's/^.*@([-.a-zA-Z0-9]*):.*$/\1/' <<EOF
$db_url
EOF`
export PGPORT=`sed -E 's/^.*:([-.a-zA-Z0-9]*)\/.*$/\1/' <<EOF
$db_url
EOF`
export PGUSER=`sed -E 's/^.*\/\/([a-zA-Z0-9]*):.*$/\1/' <<EOF
$db_url
EOF`
export PGPASSWORD=`sed -E 's/^.*:([a-zA-Z0-9]*)@.*$/\1/' <<EOF
$db_url
EOF`

pg_dump --schema ${schema} -Osxv -f schema.sql

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