Skip to content

Instantly share code, notes, and snippets.

@rnorth
Last active December 18, 2015 10:59
Show Gist options
  • Save rnorth/5772315 to your computer and use it in GitHub Desktop.
Save rnorth/5772315 to your computer and use it in GitHub Desktop.
A 'predeploy' script for dokku to automatically create an empty postgres database for new apps. Would require pre/post deploy hook support in dokku.
#!/bin/bash
set -e
export APP_NAME=$1
export PGHOST=$(< $HOME/predeploy.d/postgres_host)
export PGUSER=admin
if [ -f $HOME/$APP_NAME/DATABASE_URL ]; then
# Database URL is already set - DB must exist
DATABASE_URL=$(cat $HOME/$APP_NAME/DATABASE_URL)
else
# New DB needs to be created
DB_NAME=$(echo $APP_NAME | sed -r 's/[^a-z0-9]/_/g')
USER_NAME=$DB_NAME
USER_PASSWORD=$(tr -dc "[:alpha:]" < /dev/urandom | head -c 16)
echo "CREATE ROLE $USER_NAME WITH LOGIN ENCRYPTED PASSWORD '$USER_PASSWORD'" | psql -h $PGHOST -d postgres -U $PGUSER
echo "CREATE DATABASE $DB_NAME WITH OWNER $USER_NAME" | psql -h $PGHOST -d postgres -U $PGUSER
DATABASE_URL="postgresql://$USER_NAME:$USER_PASSWORD@$PGHOST/$DB_NAME"
echo "$DATABASE_URL" > $HOME/$APP_NAME/DATABASE_URL
echo -n " -d -e DATABASE_URL=${DATABASE_URL} " > $HOME/$APP_NAME/CUSTOM_ENV
fi
echo " Database URL: $DATABASE_URL"
@rnorth
Copy link
Author

rnorth commented Jun 13, 2013

The -d on line 22 is a nasty 'workaround' for wierd effects when the chars '-e' appear first in a variable which gets substituted in somewhere (a tweaked deploystep script in my case). There's got to be a better solution for this!

@derveloper
Copy link

how do i integrate this?

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