#!/bin/bash set -eu DATABASE_HOST=${POSTGRES_HOST:-} DATABASE_PORT=${POSTGRES_PORT:-5432} DATABASE_SUPERUSER=${POSTGRES_USER:-postgres} DATABASE_SUPERUSER_PASSWORD=${POSTGRES_PASSWORD:-metamigo} export PGPASSWORD=$DATABASE_SUPERUSER_PASSWORD # this script is run under two circumstances: with a local postgres and a remote postgres # local postgres: we should use the unix domain socket to connect # remote postgres: we should pass the --host param HOST_PARAM="--host=" if [[ ! -z ${DATABASE_HOST} ]]; then HOST_PARAM="--host=${DATABASE_HOST}" fi # wait for postgres process to settle set +e echo "pg_isready $HOST_PARAM --username $POSTGRES_USER --dbname template1" pg_isready "$HOST_PARAM" --username "$POSTGRES_USER" --dbname template1 while ! pg_isready "$HOST_PARAM" --username "$POSTGRES_USER" --dbname template1; do echo "$(date) - waiting for database to start" sleep 10 done set -e echo echo echo "Creating the database and the roles" # We're using 'template1' because we know it should exist. We should not actually change this database. psql -Xv ON_ERROR_STOP=1 "$HOST_PARAM" --username "$POSTGRES_USER" --dbname template1 <