Skip to content

Instantly share code, notes, and snippets.

@Lucus16
Created June 3, 2026 11:52
Show Gist options
  • Select an option

  • Save Lucus16/65cb4b6bf5ca0f9f02ac87bb1f68a753 to your computer and use it in GitHub Desktop.

Select an option

Save Lucus16/65cb4b6bf5ca0f9f02ac87bb1f68a753 to your computer and use it in GitHub Desktop.
simple temp postgres database
#!/usr/bin/env bash
# A simple wrapper around pg_ctl that automatically initializes and creates a database on start.
# export PGHOST=$(mktemp -d)
# pg_tmp start
# psql -c "SELECT 'Have fun!';"
# pg_tmp stop
# rm -r $PGHOST # Be careful!
set -eo pipefail
PGDATA="$(realpath --canonicalize-missing "$PGHOST")"
export PGDATA
case "$1" in
start|restart)
mkdir -p "$PGDATA"
[ -f "$PGDATA/PG_VERSION" ] || \
initdb --auth-local=peer --auth-host=reject >/dev/null
pg_ctl --log="$PGDATA/postgres.log" --wait \
-o "-c listen_addresses= -c unix_socket_directories=$PGDATA" "$@"
psql --command= 2>/dev/null || createdb
;;
*)
pg_ctl "$@"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment