Last active
February 1, 2023 22:17
-
-
Save CMCDragonkai/7bbe838bcb48bbe3ed3819adf0e88919 to your computer and use it in GitHub Desktop.
PostgreSQL in Current Working Directory #postgresql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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