#!/usr/bin/env bash # Starts a throwaway PostgreSQL 14 on port 55432 with no Docker and no root. # # On a Debian/Ubuntu machine the .deb packages can simply be downloaded and unpacked into a # prefix; PostgreSQL does not need to be installed system-wide and refuses to run as root anyway. # This is how docs/output/ was produced. If you have Docker, docker-compose.yml is easier. # # scripts/postgres.sh start|stop|psql set -euo pipefail cd "$(dirname "$0")/.." PREFIX="$PWD/target/pg" PGROOT="$PREFIX/root" PGDATA="$PREFIX/data" export PATH="$PGROOT/usr/lib/postgresql/14/bin:$PATH" export LD_LIBRARY_PATH="$PGROOT/usr/lib/x86_64-linux-gnu:$PGROOT/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH:-}" install_pg() { mkdir -p "$PREFIX/debs" "$PGROOT" ( cd "$PREFIX/debs" for p in postgresql-14 postgresql-client-14 postgresql-common postgresql-client-common \ libpq5 libllvm14 libicu70 libssl3 libxslt1.1 libxml2 libedit2 libbsd0 libmd0 \ zlib1g liblz4-1 libzstd1 libtinfo6 libncurses6 libcom-err2 libkrb5-3 libk5crypto3 \ libkrb5support0 libgssapi-krb5-2 libkeyutils1 libsasl2-2 libldap-2.5-0 libgnutls30 \ libnettle8 libhogweed6 libgmp10 libp11-kit0 libtasn1-6 libidn2-0 libunistring2 libffi8; do apt-get download "$p" >/dev/null 2>&1 || true done for d in *.deb; do dpkg -x "$d" "$PGROOT" 2>/dev/null || true; done ) } case "${1:-start}" in start) [ -x "$PGROOT/usr/lib/postgresql/14/bin/initdb" ] || install_pg if [ ! -d "$PGDATA" ]; then initdb -D "$PGDATA" -U app --auth=trust -E UTF8 >/dev/null fi pg_ctl -D "$PGDATA" -o "-p 55432 -k $PREFIX -c listen_addresses=127.0.0.1" \ -l "$PREFIX/pg.log" start >/dev/null 2>&1 || true for _ in $(seq 1 30); do pg_isready -h 127.0.0.1 -p 55432 >/dev/null 2>&1 && break; sleep 1; done psql -h 127.0.0.1 -p 55432 -U app -d postgres -tc \ "select 1 from pg_database where datname='shedlockdemo'" | grep -q 1 || \ psql -h 127.0.0.1 -p 55432 -U app -d postgres -q -c "create database shedlockdemo" pg_isready -h 127.0.0.1 -p 55432 ;; stop) pg_ctl -D "$PGDATA" stop -m fast >/dev/null 2>&1 || true ;; psql) shift; psql -h 127.0.0.1 -p 55432 -U app -d shedlockdemo "$@" ;; *) echo "usage: $0 start|stop|psql" >&2; exit 2 ;; esac