Skip to content

Instantly share code, notes, and snippets.

@aaronlelevier
Created January 10, 2018 18:08
Show Gist options
  • Select an option

  • Save aaronlelevier/a6b6947f83d3989f247c8de3e22c64e7 to your computer and use it in GitHub Desktop.

Select an option

Save aaronlelevier/a6b6947f83d3989f247c8de3e22c64e7 to your computer and use it in GitHub Desktop.
Bash functions for backing up and restoring any database in PostgreSQL by name
backupdb () {
if [ $# -eq 0 ]
then
echo "db_name not specified"
else
db_name=$1
db_name_copy=${db_name}_copy
psql -c "DROP DATABASE $db_name_copy;"
psql -c "CREATE DATABASE $db_name_copy WITH TEMPLATE $db_name;"
fi
}
restoredb () {
if [ $# -eq 0 ]
then
echo "db_name not specified"
else
db_name=$1
db_name_copy=${db_name}_copy
psql -c "DROP DATABASE $db_name;"
psql -c "CREATE DATABASE $db_name WITH TEMPLATE $db_name_copy;"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment