Skip to content

Instantly share code, notes, and snippets.

@PeloNZ
Last active August 29, 2015 14:19
Show Gist options
  • Save PeloNZ/80c260b7b7f8dbab5280 to your computer and use it in GitHub Desktop.
Save PeloNZ/80c260b7b7f8dbab5280 to your computer and use it in GitHub Desktop.
restore postgres db dump
#!/bin/bash
set -x
set -e
if [ $1 ] ; then
DB=$1
fi
if [ $2 ]; then
FILE=$2
fi
if [ $3 ]; then
LIST="-L $3"
fi
# drop connections
psql -ec "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$DB' AND pid <> pg_backend_pid();" postgres
dropdb $DB
createdb -O $DB $DB
pg_restore -vO -j4 $LIST -U $DB -d $DB $FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment