Files

22 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Starts a throwaway PostgreSQL server for the tests, without Docker. Idempotent.
# PostgreSQL 18.6 : zonky embedded-postgres-binaries-linux-amd64 18.6.0 from Maven Central (initdb, pg_ctl, postgres), port 55432
# PostgreSQL refuses to run as root, so this creates a throwaway OS user (pgj). Run it as root in a disposable sandbox.
set -euo pipefail
BASE="${BASE:-/tmp/work/dbs}"
PGPORT="${PGPORT:-55432}"
mkdir -p "$BASE/pg"
id pgj >/dev/null 2>&1 || useradd -m pgj
if [ ! -x "$BASE/pg/pg/bin/postgres" ]; then
curl -sSfL -o "$BASE/pg/pg.jar" \
https://repo1.maven.org/maven2/io/zonky/test/postgres/embedded-postgres-binaries-linux-amd64/18.6.0/embedded-postgres-binaries-linux-amd64-18.6.0.jar
(cd "$BASE/pg" && unzip -o -q pg.jar && mkdir -p pg && tar -xJf postgres-linux-x86_64.txz -C pg)
fi
chmod -R a+rX "$BASE/pg"
if [ ! -f "$BASE/pg/data/PG_VERSION" ]; then
mkdir -p "$BASE/pg/data"; chown pgj "$BASE/pg/data"
su pgj -c "cd /tmp && $BASE/pg/pg/bin/initdb -D $BASE/pg/data -U postgres -A trust -E UTF8 >/dev/null"
fi
su pgj -c "$BASE/pg/pg/bin/pg_ctl -D $BASE/pg/data -o '-p $PGPORT -k /tmp' -l $BASE/pg/data/pg.log -w start" || true
psql -h 127.0.0.1 -p "$PGPORT" -U postgres -Atc 'select version()'