Skip to content

Instantly share code, notes, and snippets.

@byF
Created June 1, 2020 10:21
Show Gist options
  • Save byF/ac877f903e8f54ad2623c6a13fd1cb5e to your computer and use it in GitHub Desktop.
Save byF/ac877f903e8f54ad2623c6a13fd1cb5e to your computer and use it in GitHub Desktop.
wait-for-pg script which can be used as Docker entrypoint
#!/usr/bin/env bash
set -e
if [ -z ${DATABASE_HOST+x} ]
then
echo "$0: Provide DATABASE_HOST environment variable" >&2
exit 1
fi
attempt=1
until PGPASSWORD="${DATABASE_PASS}" \
pg_isready \
-h "${DATABASE_HOST}" \
-p "${DATABASE_PORT:="5432"}" \
-U "${DATABASE_USER:="postgres"}"; do
attempts=$((attempts+1))
if [ $attempts -eq ${RECONNECT_ATTEMPTS:=3} ]
then
echo "$0: Failed to reach the database at ${DATABASE_HOST}" >&2
exit 1
fi
sleep 5
done
exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment