Skip to content

Instantly share code, notes, and snippets.

Created February 7, 2013 16:38
Show Gist options
  • Save anonymous/4732222 to your computer and use it in GitHub Desktop.
Save anonymous/4732222 to your computer and use it in GitHub Desktop.
Create Postgress Database through Shell
#!/bin/bash
username=$1
password=$2
if [[ ! $2 ]]; then
read -sp "Password for $1: " password
echo
fi
if [[ $3 ]]; then
database=$3
else
database=$1
fi
if [[ ! $username && ! $password ]]; then
echo "Usage: $0 username password databasename"
echo " - databasename is optional defaults to username"
exit 1
fi
read -p "Creating database $database for $username: " answer
if [[ $answer == "y" || $answer == 'yes' ]]; then
echo "Creating database $database"
psql postgres -c "CREATE DATABASE $database"
psql postgres -c "CREATE USER $username WITH PASSWORD '$password'"
psql postgres -c "GRANT ALL PRIVILEGES ON DATABASE $database to $username"
echo "Finished ..."
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment