Skip to content

Instantly share code, notes, and snippets.

@marianobrc
Last active February 17, 2022 23:25
Show Gist options
  • Save marianobrc/ecc37d49327cbe1f874ee3f40135efef to your computer and use it in GitHub Desktop.
Save marianobrc/ecc37d49327cbe1f874ee3f40135efef to your computer and use it in GitHub Desktop.
#!/bin/bash
# When starting the django app container, we need to wait until the postgress DB is ready to receive connections
# docker-compose "depends_on: - db" checks the container started, but is not enough to check that the database is ready to take connections
# This script also accepts a command to be executed after the DB is ready (i.e. migrate, runserver or a script..)
function postgres_ready(){
python << END
import sys
import psycopg2
try:
print("Trying to connect to database '$DB_NAME' on host '$DB_HOST'..")
conn = psycopg2.connect(dbname="$DB_NAME", user="$DB_USER", password="$DB_PASSWORD", host="$DB_HOST")
except psycopg2.OperationalError as e:
print(e)
sys.exit(-1)
sys.exit(0)
END
}
until postgres_ready; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - continuing..."
# Here the received command is executed
exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment