mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-12-18 14:06:37 +00:00
Update DB script.
This commit is contained in:
parent
7aaf49585d
commit
0de778e218
1 changed files with 12 additions and 2 deletions
|
@ -3,17 +3,26 @@ set -x
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
# Check if a custom user has been set, otherwise default to 'postgres'
|
# Check if a custom user has been set, otherwise default to 'postgres'
|
||||||
DB_USER=${POSTGRES_USER:=postgres}
|
DB_USER="${POSTGRES_USER:=postgres}"
|
||||||
# Check if a custom password has been set, otherwise default to 'password'
|
# Check if a custom password has been set, otherwise default to 'password'
|
||||||
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
|
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
|
||||||
# Check if a custom password has been set, otherwise default to 'newsletter'
|
# Check if a custom password has been set, otherwise default to 'newsletter'
|
||||||
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}" ]]
|
||||||
then
|
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
|
# Launch postgres using Docker
|
||||||
docker run \
|
docker run \
|
||||||
-e POSTGRES_USER=${DB_USER} \
|
-e POSTGRES_USER=${DB_USER} \
|
||||||
|
@ -21,12 +30,13 @@ then
|
||||||
-e POSTGRES_DB=${DB_NAME} \
|
-e POSTGRES_DB=${DB_NAME} \
|
||||||
-p "${DB_PORT}":5432 \
|
-p "${DB_PORT}":5432 \
|
||||||
-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
|
||||||
|
|
Loading…
Reference in a new issue