# Expose a /db-health endpoint that your app already serves.
# Monitor that URL, not the DB directly, so the check sees
# the real connection path (pooler, SSL, auth, DNS).
from fastapi import APIRouter, Response
import psycopg
router = APIRouter()
@router.get("/db-health")
async def db_health(response: Response):
try:
async with await psycopg.AsyncConnection.connect(
DATABASE_URL, connect_timeout=3
) as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 1, pg_is_in_recovery()")
row = await cur.fetchone()
return {"status": "ok", "replica": row[1]}
except Exception as e:
response.status_code = 503
return {"status": "down", "error": str(e)[:120]}