#!/bin/sh set -e # Extract host and port from DATABASE_URL # Format: postgresql+asyncpg://user:pass@host:port/dbname DB_HOST=$(echo "$DATABASE_URL" | sed -E 's|.*@([^/:]+).*|\1|') DB_PORT=$(echo "$DATABASE_URL" | sed -E 's|.*@[^/:]+:([0-9]+)/.*|\1|') if [ -z "$DB_PORT" ] || [ "$DB_PORT" = "$DB_HOST" ]; then DB_PORT="5432" fi echo "Waiting for postgres at $DB_HOST:$DB_PORT ..." max_retries=30 counter=0 until pg_isready -h "$DB_HOST" -p "$DB_PORT" -q 2>/dev/null; do counter=$((counter + 1)) if [ $counter -ge $max_retries ]; then echo "ERROR: postgres at $DB_HOST:$DB_PORT not ready after ${max_retries}s" exit 1 fi echo " postgres not ready, retrying in 2s ... ($counter/$max_retries)" sleep 2 done echo "postgres is ready!" exec "$@"