Last active
May 9, 2017 04:01
-
-
Save bdunn313/ed74cf504a0aad56daae1f0c9915fc80 to your computer and use it in GitHub Desktop.
pg-quick - Quick n Dirty script for standing up a local PostgreSQL DB & User
This file contains 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 | |
# Simple script to run the createuser and createdb | |
# commands after a local/server install of PostgreSQL | |
# Exit as soon as any line in the bash script fails | |
# & prints each command executed (prefix with ++) | |
set -e | |
# Make sure we have a name parameter | |
if [ $# -lt 1 -o $# -gt 2 ] | |
then | |
echo "ERROR: You must specify a name" | |
echo "USAGE: psquick name [dbname]" | |
echo " (if dbname is not specified, name is used for both username and database name)" | |
exit 1 | |
fi | |
# Alias parameters for easy peasy reading :P | |
# Highly unnecessary step | |
# | |
# For that matter I'm a little comment happy too eh? | |
username=$1 | |
if [ $# -eq 2 ] | |
then | |
dbname=$2 | |
else | |
dbname=$1 | |
fi | |
# Create the user | |
echo "Creating user named $username" | |
createuser $username -P | |
# Create the DB | |
echo "Creating database named $dbname" | |
createdb $dbname | |
# Grant User Privelages | |
echo "Granting user '$username' privelages on database '$dbname'" | |
psql --dbnam=$dbname --command="GRANT ALL PRIVILEGES ON DATABASE $dbname to $username" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage