#!/bin/sh set -euo pipefail kill_jobs() { jobs -p | xargs -rn1 kill || true } trap "kill_jobs" EXIT POSTGRES="postgres -c unix_socket_directories=${PPQ_POSTGRES_DATA_DIR}" # NOTE: Environment variables here are set up by .envrc postgres_init () { local PGDATA_NEW="${PGDATA}.new" rm -rf "${PGDATA_NEW}" pg_ctl -s init -D "${PGDATA_NEW}" -o "-E UTF-8 --no-locale -A trust -U postgres" echo "${PGDATA_NEW}" # Clobber postgresql.conf ln -fs "${PPQ_POSTGRES_DIR}/postgresql.conf" "${PGDATA_NEW}/" ln -fs "${PPQ_POSTGRES_DIR}/postgresql_overrides.conf" "${PGDATA_NEW}/" || true if [ -f "${PPQ_POSTGRES_DIR}/prepare.sql" ]; then $POSTGRES --single -D "${PGDATA_NEW}" postgres < "${PPQ_POSTGRES_DIR}/prepare.sql" >/dev/null fi mv "${PGDATA_NEW}" "${PGDATA}" sync } postgres_start () { [ -d "${PGDATA}" ] || postgres_init $POSTGRES } COMMAND="${1:-}" [ -n "$COMMAND" ] && shift case "$COMMAND" in start) postgres_start ;; *) echo "Available commands:" echo " start - start the PostgreSQL server" ;; esac