postgres.sh 465 B

123456789101112131415161718192021
  1. #!/bin/bash
  2. set -eo pipefail
  3. host="$(hostname -i || echo '127.0.0.1')"
  4. user="${POSTGRES_USER:-postgres}"
  5. db="${POSTGRES_DB:-$POSTGRES_USER}"
  6. export PGPASSWORD="${POSTGRES_PASSWORD:-}"
  7. args=(
  8. # force postgres to not use the local unix socket (test "external" connectibility)
  9. --host "$host"
  10. --username "$user"
  11. --dbname "$db"
  12. --quiet --no-align --tuples-only
  13. )
  14. if select="$(echo 'SELECT 1' | psql "${args[@]}")" && [ "$select" = '1' ]; then
  15. exit 0
  16. fi
  17. exit 1