#!/bin/sh set -e # Wait for PostgreSQL to be ready # Usage: wait-for-db.sh [host] [port] [user] [database] host="${DB_HOST:-db}" port="${DB_PORT:-5432}" user="${DB_USER:-postgres}" database="${DB_NAME:-financial_data}" max_attempts=30 attempt=0 echo "Waiting for PostgreSQL to be ready at ${host}:${port}..." while [ $attempt -lt $max_attempts ]; do if PGPASSWORD="${DB_PASSWORD}" pg_isready -h "$host" -p "$port" -U "$user" -d "$database" >/dev/null 2>&1; then echo "PostgreSQL is ready!" exit 0 fi attempt=$((attempt + 1)) echo "Attempt $attempt/$max_attempts: PostgreSQL is not ready yet. Waiting..." sleep 2 done echo "PostgreSQL failed to become ready after $max_attempts attempts" exit 1