aboutsummaryrefslogtreecommitdiff
path: root/ppq.sh
diff options
context:
space:
mode:
authorPeter Bex <peter@more-magic.net>2023-03-19 10:06:45 +0100
committerPeter Bex <peter@more-magic.net>2023-03-19 10:46:25 +0100
commit9d05c9ca73c2b501744d528475532ab728318bde (patch)
tree66290d3bf9974ab7cf418508cc0937b838fd0343 /ppq.sh
downloadppq-9d05c9ca73c2b501744d528475532ab728318bde.tar.gz
Initial version of "ppq" - portable PostgreSQL
This allows running PostgreSQL locally without having to set up a system-wide service. It is portable in the sense that you can move the directory around, and you can run multiple instances side-by-side.
Diffstat (limited to 'ppq.sh')
-rwxr-xr-xppq.sh50
1 files changed, 50 insertions, 0 deletions
diff --git a/ppq.sh b/ppq.sh
new file mode 100755
index 0000000..29a9d5e
--- /dev/null
+++ b/ppq.sh
@@ -0,0 +1,50 @@
+#!/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