Skip to content

Instantly share code, notes, and snippets.

@arkadius
Created September 25, 2024 13:37
Show Gist options
  • Save arkadius/652d911ec4394e6b974bcbbae0dcc532 to your computer and use it in GitHub Desktop.
Save arkadius/652d911ec4394e6b974bcbbae0dcc532 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
RESULT=$(psql -tc "SELECT 1 FROM pg_database WHERE datname = '$DB';")
if [ -z "$RESULT" ]; then
echo "Database $DB does not exist. Creating..."
psql -v ON_ERROR_STOP=1 <<-EOSQL
CREATE USER $USER WITH ENCRYPTED PASSWORD '$PASSWORD';
CREATE DATABASE $DB;
ALTER DATABASE $DB OWNER TO $USER;
EOSQL
else
echo "Database $DB already exists."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment