From ac90bcbb79827d1fbd39a77ef17e196c61c6077f Mon Sep 17 00:00:00 2001 From: dkulla01 Date: Fri, 2 Apr 2021 06:41:14 -0400 Subject: [PATCH] Exit if dockerized postgres is already running (#86) This creates the postgres container with a "postgres_" 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 --- scripts/init_db.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/scripts/init_db.sh b/scripts/init_db.sh index 5a0c806..2a773b2 100755 --- a/scripts/init_db.sh +++ b/scripts/init_db.sh @@ -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