Skip to content

Instantly share code, notes, and snippets.

@chocobn69
Created December 18, 2019 08:59
Show Gist options
  • Save chocobn69/ed5f6cad1cfe39405bd446afaf321440 to your computer and use it in GitHub Desktop.
Save chocobn69/ed5f6cad1cfe39405bd446afaf321440 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -eo pipefail
host="$(hostname --ip-address || echo '127.0.0.1')"
user="${POSTGRES_USER:-postgres}"
db="${POSTGRES_DB:-$POSTGRES_USER}"
export PGPASSWORD="${POSTGRES_PASSWORD:-}"
args=(
# force postgres to not use the local unix socket (test "external" connectibility)
--host "$host"
--username "$user"
--dbname "$db"
--quiet --no-align --tuples-only --no-psqlrc
)
if select="$(echo 'SELECT 1;' | psql "${args[@]}"| xargs echo -n )" && [ "$select" = '1' ]; then
exit 0
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment