Waiting for database


I found that when I spin up a postgresql image, and run tests - the tests run before the database is fully up. Is there a way to add a step that waits for the database to be ready?


until psql -h $PG_HOST -U $PG_USER -d $PG_DATABASE -c "select 1" > /dev/null 2>&1 || [ $TIMEOUT -eq 0 ]; do
  echo "Waiting for postgres server, $((TIMEOUT--)) remaining attempts..."
  sleep 1