Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active February 1, 2023 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/7bbe838bcb48bbe3ed3819adf0e88919 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/7bbe838bcb48bbe3ed3819adf0e88919 to your computer and use it in GitHub Desktop.
PostgreSQL in Current Working Directory #postgresql
#!/usr/bin/env bash
# for when you want to start postgresql in a current working directory
# externally set environment variables
# any variable that is unset will be defaulted
# if the variable is set and an empty string, it will be used literally
export PGDATABASE=postgres
export PGUSER=postgres # this needs to be set your OS username! (otherwise psql will complain about missing role)
export PGPASSWORD=
export PGHOST=localhost
export PGPORT=5432
# script
export PGDATA="$(pwd)/.pgdata"
mkdir --parents "$PGDATA"
initdb
if ! pg_ctl status; then
# this will start postgres and wait for it to accept connections
# it explicitly sets PGHOST because if it is an empty string
# then we assume postgres will only use unix domain sockets
# all other standard environment variables will be used as normal
# after it is started, this command exits
pg_ctl start -w -o "-h '$PGHOST'"
fi
# use pg_ctl stop to stop the database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment