Created
September 8, 2023 16:38
-
-
Save theothermattm/9cb80e3b6dc90f84af77c5d3d63bea9e to your computer and use it in GitHub Desktop.
Simple bash script to restore a postgres backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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