#!/bin/bash

set -x

ip_address=$(hostname -i)
timeout=${1:-60}

echo Attempting connection to Postgres for $timeout seconds...
for _ in $(seq 1 $timeout); do
  gosu postgres pg_isready --host="$ip_address" && echo "Postgres connected!" && exit
  sleep 1
done

echo Postgres is still down after $timeout seconds.
exit 1
