pg_isready 548 Bytes
#!/bin/sh
set -ex

# POSTGRES_HOST
# POSTGRES_USER
# POSTGRES_DATABASE
# PGPASSWORD

check_pg() {
	pg_isready -h "${POSTGRES_HOST}" -U "${POSTGRES_USER}"
}

check_auth() {
	psql -h "${POSTGRES_HOST}" -U "${POSTGRES_USER}" "${POSTGRES_DATABASE}"
}

loop() {
	checker="$1"
	message="$2"
	while ! $checker; do
		echo "$message" 1>&2
		sleep 1
		count=5
		while [ $count -gt 0 ] && ! $checker; do
			count=$(($count - 1))
			sleep 1
		done
	done
}

loop check_pg "Waiting for database to be up"
loop check_auth "Waiting for user+password to be active"