Last active
November 7, 2022 05:17
-
-
Save luzfcb/2a70029bd4bd09182e8939b38d31d64e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -o errexit | |
set -o pipefail | |
set -o nounset | |
postgres_ready() { | |
python << END | |
import sys | |
import psycopg2 | |
import dj_database_url | |
DATABASE = dj_database_url.config('DATABASE_URL') | |
print(DATABASE) | |
try: | |
psycopg2.connect( | |
dbname=DATABASE["NAME"], | |
user=DATABASE["USER"], | |
password=DATABASE["PASSWORD"], | |
host=DATABASE["HOST"], | |
port=DATABASE["PORT"], | |
) | |
except psycopg2.OperationalError: | |
sys.exit(-1) | |
sys.exit(0) | |
END | |
} | |
until postgres_ready; do | |
>&2 echo 'Waiting for PostgreSQL to become available...' | |
sleep 1 | |
done | |
>&2 echo 'PostgreSQL is available' | |
exec "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eo pipefail | |
# based on https://github.com/docker-library/healthcheck/blob/master/postgres/docker-healthcheck | |
# If the healthcheck is configured on the Dockerfile or docker-compose.yml | |
# use the docker inspect to show the healthcheck logs: | |
# docker inspect --format "{{json .State.Health }}" <container name> | jq | |
host="$(hostname -i || echo '127.0.0.1')" | |
user="${POSTGRES_USER:-postgres}" | |
db="${POSTGRES_DB:-POSTGRES_USER}" | |
export PGPASSWORD="${POSTGRES_PASSWORD:-}" | |
args=( | |
# force postgres to not use the local unix socket (test "external" connectibility) | |
--host "$host" | |
--username "$user" | |
--dbname "$db" | |
--quiet --no-align --tuples-only | |
) | |
if select="$(echo 'SELECT 1' | psql "${args[@]}")" && [ "$select" = '1' ]; then | |
echo "PostgreSQL is running as expected."; | |
exit 0; | |
fi | |
echo "PostgreSQL is down."; | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment