PostgreSQL¶
test-postgres-connectivity¶
Runs five checks in order: DNS resolution, TCP reachability, a psql login that runs SELECT 1, a TLS check with openssl, and the first five hops of a traceroute. It stops at the first failed DNS or TCP check and skips a check when its tool is not installed. It exits with status 1 if the DNS, TCP, or psql check fails.
Set connection details with DB_HOST, DB_PORT, DB_USER, and DB_NAME. The script never prompts for a password: it reads PGPASSWORD, or ~/.pgpass if that is empty.1 It needs no root, but you need the database credentials, which are usually in a root only file such as Workbench's database.conf.
Source: scripts/test-postgres-connectivity
#!/bin/bash
#
# test-postgres-connectivity: Test PostgreSQL database connectivity
#
# Usage:
# DB_HOST=db.example.com DB_USER=admin DB_NAME=rstudio_pm \
# test-postgres-connectivity
#
# Supply the password with PGPASSWORD or a ~/.pgpass file. The script never
# prompts. The exit status is 0 only when every check that ran passed.
#
set -uo pipefail
# Color only on a terminal, so saved output has no escape codes
if [[ -t 1 ]]; then
readonly RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m' NC='\033[0m'
else
readonly RED='' GREEN='' YELLOW='' BLUE='' NC=''
fi
readonly DB_HOST="${DB_HOST:-postgres.example.com}"
readonly DB_PORT="${DB_PORT:-5432}"
readonly DB_USER="${DB_USER:-posit_admin}"
readonly DB_NAME="${DB_NAME:-rstudio_pm}"
readonly RULE='========================================'
failed=0
#######################################
# Print a colored banner.
# Arguments:
# Title.
#######################################
banner() {
echo -e "${BLUE}${RULE}${NC}"
echo -e "${BLUE} $1${NC}"
echo -e "${BLUE}${RULE}${NC}"
echo ""
}
#######################################
# Stop unless the database host name resolves.
# getent handles names, IPv4 and IPv6 literals, and needs no bind-utils.
#######################################
check_dns() {
local ip
echo -e "${BLUE}[1] Testing DNS resolution...${NC}"
ip="$(getent ahosts "${DB_HOST}" 2>/dev/null | awk 'NR == 1 { print $1 }')"
if [[ -n "${ip}" ]]; then
echo -e "${GREEN}✓ DNS resolution successful: ${ip}${NC}"
else
echo -e "${RED}✗ DNS resolution failed for ${DB_HOST}${NC}"
exit 1
fi
echo ""
}
#######################################
# Stop unless a TCP connection opens to the database port.
# Bash /dev/tcp needs no extra package; the exit status is the result.
#######################################
check_tcp() {
echo -e "${BLUE}[2] Testing TCP connection to ${DB_HOST}:${DB_PORT}...${NC}"
# The inner shell expands $1 and $2, so single quotes are intended
# shellcheck disable=SC2016
if timeout 10 bash -c 'echo > "/dev/tcp/$1/$2"' _ "${DB_HOST}" "${DB_PORT}" \
2>/dev/null; then
echo -e "${GREEN}✓ TCP connection successful${NC}"
else
echo -e "${RED}✗ TCP connection failed${NC}"
echo "This could indicate:"
echo " - Firewall blocking outbound port ${DB_PORT}"
echo " - Database firewall rules blocking this IP"
echo " - Network routing issue"
exit 1
fi
echo ""
}
#######################################
# Run a query with psql; -w never prompts for a password.
# Globals:
# failed
#######################################
check_psql() {
local conn=(
-w -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}"
)
echo -e "${BLUE}[3] Testing PostgreSQL client connection...${NC}"
if ! command -v psql >/dev/null 2>&1; then
echo -e "${YELLOW}⚠ psql not installed, skipping PostgreSQL client" \
"test${NC}"
echo "Ask your sudo administrator to install the postgresql package"
echo ""
return
fi
echo "Attempting to connect (uses PGPASSWORD or ~/.pgpass, no prompt)..."
export PGCONNECT_TIMEOUT=10
if psql "${conn[@]}" -c "SELECT 1;" -t >/dev/null 2>&1; then
echo -e "${GREEN}✓ PostgreSQL connection successful${NC}"
else
echo -e "${RED}✗ PostgreSQL connection failed${NC}"
failed=1
echo "Error details:"
psql "${conn[@]}" -c "SELECT version();" 2>&1 | head -10 | sed 's/^/ /'
fi
echo ""
}
#######################################
# Try a TLS handshake. Advisory only: it does not set
# -verify_return_error and does not affect the exit status.
#######################################
check_tls() {
local tls_output
echo -e "${BLUE}[4] Testing SSL/TLS connection...${NC}"
if ! command -v openssl >/dev/null 2>&1; then
echo -e "${YELLOW}⚠ openssl not installed, skipping SSL test${NC}"
echo ""
return
fi
tls_output="$(timeout 10 openssl s_client -connect "${DB_HOST}:${DB_PORT}" \
-starttls postgres </dev/null 2>&1)"
if grep -q "Verification: OK\|Verify return code: 0" <<< "${tls_output}"
then
echo -e "${GREEN}✓ SSL/TLS connection successful${NC}"
else
echo -e "${YELLOW}⚠ SSL/TLS verification inconclusive${NC}"
echo "Note: some managed PostgreSQL services require SSL connections"
fi
echo ""
}
#######################################
# Print the first hops to the database host.
#######################################
check_route() {
echo -e "${BLUE}[5] Testing network route...${NC}"
if command -v traceroute >/dev/null 2>&1; then
echo "First 5 hops to database:"
traceroute -m 5 -w 2 "${DB_HOST}" 2>&1 | head -6 | sed 's/^/ /'
else
echo -e "${YELLOW}⚠ traceroute not installed, skipping route test${NC}"
fi
echo ""
}
main() {
banner "PostgreSQL Connectivity Test"
echo "Testing connection to:"
echo " Host: ${DB_HOST}"
echo " Port: ${DB_PORT}"
echo " User: ${DB_USER}"
echo " Database: ${DB_NAME}"
echo ""
check_dns
check_tcp
check_psql
check_tls
check_route
banner "Test Summary"
echo "If DNS resolution and TCP connection succeed but PostgreSQL"
echo "connection fails, check:"
echo " 1. Database firewall rules allow your VM's IP"
echo " 2. Password is correct"
echo " 3. Database user has proper permissions"
echo " 4. SSL certificate validation"
exit "${failed}"
}
main "$@"