45 lines
1.8 KiB
Bash
45 lines
1.8 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
PGDATA="${PGDATA:-/var/lib/postgresql/data}"
|
|
PGUSER="${PGUSER:-lmr}"
|
|
PGPASSWORD="${PGPASSWORD:-lmr}"
|
|
PGDATABASE="${PGDATABASE:-lmr}"
|
|
|
|
# ── Initialise cluster if needed ─────────────────────────────────────────────
|
|
if [ ! -f "$PGDATA/PG_VERSION" ]; then
|
|
echo "→ initialising postgres cluster"
|
|
install -d -m 0700 -o postgres "$PGDATA"
|
|
su-exec postgres initdb \
|
|
--username="$PGUSER" \
|
|
--pwfile=<(echo "$PGPASSWORD") \
|
|
--auth=md5 \
|
|
-D "$PGDATA" \
|
|
--no-instructions
|
|
fi
|
|
|
|
# ── Start postgres in the background ─────────────────────────────────────────
|
|
echo "→ starting postgres"
|
|
su-exec postgres postgres -D "$PGDATA" \
|
|
-c listen_addresses=localhost \
|
|
-c log_min_messages=WARNING &
|
|
PG_PID=$!
|
|
|
|
# ── Wait for postgres to accept connections ───────────────────────────────────
|
|
echo "→ waiting for postgres"
|
|
until su-exec postgres pg_isready -U "$PGUSER" -d "$PGDATABASE" -q 2>/dev/null; do
|
|
sleep 0.5
|
|
done
|
|
|
|
# ── Ensure the application database exists ───────────────────────────────────
|
|
su-exec postgres psql -U "$PGUSER" -tc \
|
|
"SELECT 1 FROM pg_database WHERE datname = '$PGDATABASE'" \
|
|
| grep -q 1 || su-exec postgres createdb -U "$PGUSER" "$PGDATABASE"
|
|
|
|
# ── Start the gateway (foreground, takes over the process) ───────────────────
|
|
echo "→ starting lmr"
|
|
# Forward signals to both processes on exit.
|
|
trap 'kill $PG_PID 2>/dev/null; wait $PG_PID' TERM INT
|
|
|
|
exec /usr/local/bin/lmr
|