Created
February 7, 2013 16:38
-
-
Save anonymous/4732222 to your computer and use it in GitHub Desktop.
Create Postgress Database through Shell
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 | |
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