pg_isready 789 Bytes
#!/bin/sh

set -xe
_msg() {
  column_count=${#1}
  echo "$1" 1>&2
  _dotted=
}
_dot() {
  [ "z$_dotted" = "z" ] && echo -n " " 1>&2 && _dotted=1
  echo -n "." 1>&2
  column_count=$(($column_count + 1))
  if [ $column_count = 20 ]; then
    echo
    column_count=1
  fi
}
_done() {
  echo " done." 1>&2
  column_count=1
}
_msg "Waiting for postgres to acception connections:"
while :; do
  tries=10
  while [ $tries -gt 0 ]; do
    if pg_isready -h "$PGHOST" 1>/dev/null 2>/dev/null; then break 2; fi
    sleep 1
    tries=$(($tries - 1))
  done
  _dot
done
_done
_msg "Waiting for $PGUSER@$PGDATABASE to be available:"
while :; do
  tries=10
  while [ $tries -gt 0 ]; do
    if psql 1>/dev/null 2>/dev/null; then break 2; fi
    sleep 1
    tries=$(($tries - 1))
  done
  _dot
done
_done