Skip to content

Instantly share code, notes, and snippets.

@theothermattm
Created September 8, 2023 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theothermattm/9cb80e3b6dc90f84af77c5d3d63bea9e to your computer and use it in GitHub Desktop.
Save theothermattm/9cb80e3b6dc90f84af77c5d3d63bea9e to your computer and use it in GitHub Desktop.
Simple bash script to restore a postgres backup
#!/bin/bash
# helper script to import a postgres export locally
if [[ -z $1 || -z $2 ]];
then
echo 'example usage: ./database-import.sh name-of-export.sh database_name'
exit 1
fi
set -o nounset
set -o errexit
EXPORT_FILE=$1
DATABASE_NAME=$2
if [[ -z $PGPASSWORD ]];
then
echo 'Please make sure you run export PGPASSWORD=yourpasswordhere before running the script'
exit 1
fi
echo "Restoring backup of $EXPORT_FILE to localhost database $DATABASE_NAME..."
psql -h localhost -U postgres -c "DROP DATABASE $DATABASE_NAME" || echo 'Database did not exist, creating...'
psql -h localhost -U postgres -c "CREATE DATABASE $DATABASE_NAME"
psql -h localhost -U postgres -d $DATABASE_NAME < $EXPORT_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment