Skip to content

Instantly share code, notes, and snippets.

@bradparker
Created July 2, 2017 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradparker/bd1a94387be13edb1a62f81979353bc5 to your computer and use it in GitHub Desktop.
Save bradparker/bd1a94387be13edb1a62f81979353bc5 to your computer and use it in GitHub Desktop.
Create a DB from DATABASE_URL
#! /usr/bin/env sh
# {provider}://{user}:{password}@{host}:{port}/{dbname}
provider=`echo $DATABASE_URL | grep '://' | sed 's/^\(.*\):\/\/.*/\1/'`
url_without_provider=`echo $DATABASE_URL | sed s/"$provider:\/\/"//`
# extract the user and password (if any)
userpass=`echo $url_without_provider | grep @ | cut -d@ -f1`
pass=`echo $userpass | grep : | cut -d: -f2`
if [ -n "$pass" ]; then
user=`echo $userpass | grep : | cut -d: -f1`
else
user=$userpass
fi
url_without_userpass=`echo $url_without_provider | sed s/"$userpass@"//`
# extract the host
hostport=`echo $url_without_userpass | cut -d/ -f1`
port=`echo $hostport | grep : | cut -d: -f2`
if [ -n "$port" ]; then
host=`echo $hostport | grep : | cut -d: -f1`
else
host=$hostport
fi
dbname=`echo $url_without_userpass | sed s/"$hostport\/"//`
echo "Creating $provider database ..."
PGPASSWORD=$pass createdb \
--username=$user \
--host=$host \
--port=$port \
$dbname
echo "Database created"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment