Skip to content

Instantly share code, notes, and snippets.

@bdunn313
Last active May 9, 2017 04:01
Show Gist options
  • Save bdunn313/ed74cf504a0aad56daae1f0c9915fc80 to your computer and use it in GitHub Desktop.
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
#!/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"
@bdunn313
Copy link
Author

bdunn313 commented May 9, 2017

Usage

# Create user & db all with the same name
pg-quick name

# Create user & db with different names
pg-quick username dbname

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment