Update db script.

This commit is contained in:
LukeMathWalker 2021-04-02 11:51:23 +01:00
parent ac90bcbb79
commit 50798e8e67

View file

@ -10,6 +10,8 @@ DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
DB_NAME="${POSTGRES_DB:=newsletter}" DB_NAME="${POSTGRES_DB:=newsletter}"
# Check if a custom port has been set, otherwise default to '5432' # Check if a custom port has been set, otherwise default to '5432'
DB_PORT="${POSTGRES_PORT:=5432}" DB_PORT="${POSTGRES_PORT:=5432}"
# Check if a custom host has been set, otherwise default to 'localhost'
DB_HOST="${POSTGRES_HOST:=localhost}"
# Allow to skip Docker if a dockerized Postgres database is already running # Allow to skip Docker if a dockerized Postgres database is already running
if [[ -z "${SKIP_DOCKER}" ]] if [[ -z "${SKIP_DOCKER}" ]]
@ -21,21 +23,20 @@ then
echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}" echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}"
exit 1 exit 1
fi fi
# Launch postgres using Docker # Launch postgres using Docker
docker run \ docker run \
-e POSTGRES_USER=${DB_USER} \ -e POSTGRES_USER=${DB_USER} \
-e POSTGRES_PASSWORD=${DB_PASSWORD} \ -e POSTGRES_PASSWORD=${DB_PASSWORD} \
-e POSTGRES_DB=${DB_NAME} \ -e POSTGRES_DB=${DB_NAME} \
-p "${DB_PORT}":5432 \ -p "${DB_PORT}":5432 \
--name "postgres_$(date '+%s')" \
-d postgres \ -d postgres \
--name "postgres_$(date '+%s')" \
postgres -N 1000 postgres -N 1000
# ^ Increased maximum number of connections for testing purposes # ^ Increased maximum number of connections for testing purposes
fi fi
# Keep pinging Postgres until it's ready to accept commands # Keep pinging Postgres until it's ready to accept commands
until PGPASSWORD="${DB_PASSWORD}" psql -h "localhost" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
>&2 echo "Postgres is still unavailable - sleeping" >&2 echo "Postgres is still unavailable - sleeping"
sleep 1 sleep 1
done done