Exit if dockerized postgres is already running (#86)

This creates the postgres container with a "postgres_<timestamp>" name.
This lets us check for an existing, running, postgres container and
exit early if it already exists. It's a little more clear/explicit than
the "port 5432 already in use" error message that results from rerunning
the script, and it addresses the gotcha in footnote 30
This commit is contained in:
dkulla01 2021-04-02 06:41:14 -04:00 committed by GitHub
parent 537f2e783e
commit ac90bcbb79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,12 +14,21 @@ DB_PORT="${POSTGRES_PORT:=5432}"
# Allow to skip Docker if a dockerized Postgres database is already running
if [[ -z "${SKIP_DOCKER}" ]]
then
# if a postgres container is running, print instructions to kill it and exit
RUNNING_POSTGRES_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.ID}}')
if [[ -n $RUNNING_POSTGRES_CONTAINER ]]; then
echo >&2 "there is a postgres container already running, kill it with"
echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}"
exit 1
fi
# Launch postgres using Docker
docker run \
-e POSTGRES_USER=${DB_USER} \
-e POSTGRES_PASSWORD=${DB_PASSWORD} \
-e POSTGRES_DB=${DB_NAME} \
-p "${DB_PORT}":5432 \
--name "postgres_$(date '+%s')" \
-d postgres \
postgres -N 1000
# ^ Increased maximum number of connections for testing purposes