#!/usr/bin/env bash capture_postgres_readiness_evidence() { local container=$1 local evidence_file=$2 docker logs "$container" >"$evidence_file" 2>&1 || true } wait_for_postgres_final_ready() { local container=$1 local evidence_file=$2 local interval_seconds=$3 local running local health while ! docker logs "$container" 2>&1 | grep -Fq 'PostgreSQL init process complete; ready for start up.'; do running=$(docker inspect --format '{{.State.Running}}' "$container" 2>/dev/null || printf missing) if [[ "$running" != true ]]; then capture_postgres_readiness_evidence "$container" "$evidence_file" echo "Isolated restore PostgreSQL exited during initialization." >&2 return 1 fi sleep "$interval_seconds" done while true; do # Docker can retain a cached healthy state from the temporary bootstrap # postmaster while that server shuts down. Query the final server directly # and proceed only after it can execute SQL. if docker exec "$container" psql \ --username postgres \ --dbname postgres \ --tuples-only \ --no-align \ --command='select 1;' >/dev/null 2>&1; then return 0 fi running=$(docker inspect --format '{{.State.Running}}' "$container" 2>/dev/null || printf missing) health=$(docker inspect \ --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}missing{{end}}' \ "$container" 2>/dev/null || printf missing) if [[ "$running" != true ]]; then capture_postgres_readiness_evidence "$container" "$evidence_file" echo "Isolated restore PostgreSQL exited before accepting SQL." >&2 return 1 fi if [[ "$health" == unhealthy ]]; then capture_postgres_readiness_evidence "$container" "$evidence_file" echo "Isolated restore PostgreSQL did not become SQL-ready." >&2 return 1 fi sleep "$interval_seconds" done }