#!/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