#!/usr/bin/env bash set -euo pipefail export PGDATA=/data/postgres if [[ ! -s "$PGDATA/PG_VERSION" ]]; then echo "Initializing Postgres data dir at $PGDATA" install -d -m 0700 -o postgres -g postgres "$PGDATA" su - postgres -c "/usr/lib/postgresql/*/bin/initdb -D '$PGDATA'" # Start temporarily on localhost to run setup su - postgres -c "/usr/lib/postgresql/*/bin/pg_ctl -D '$PGDATA' -o \"-c listen_addresses='127.0.0.1'\" -w start" DB="regula_db" USERNAME="regula" PASS="${POSTGRES_PASSWORD:-Regulapasswd#1}" su - postgres -c "psql -v ON_ERROR_STOP=1 -c \"CREATE DATABASE $DB;\"" su - postgres -c "psql -v ON_ERROR_STOP=1 -c \"CREATE USER $USERNAME WITH PASSWORD '$PASS';\"" su - postgres -c "psql -v ON_ERROR_STOP=1 -c \"GRANT ALL PRIVILEGES ON DATABASE $DB TO $USERNAME;\"" su - postgres -c "/usr/lib/postgresql/*/bin/pg_ctl -D '$PGDATA' -m fast stop" fi # Now run in foreground for supervisor exec su - postgres -c "/usr/lib/postgresql/*/bin/postgres -D '$PGDATA' -c listen_addresses='127.0.0.1'"