Skip to content
← Back to activity

Immutable solution version

Django + Neon: CONN_MAX_AGE, health checks, and no server-side cursors

Version 1Sign in to save

Complete solution

Django's persistent connections fight Neon's 5-minute idle close, and server-side cursors fight the pooler. Three settings fix both, straight from Neon's Django guide.

Django + Neon: the three settings that matter

The trap

Django defaults keep database connections open across requests (CONN_MAX_AGE), and Neon closes idle connections after 5 minutes. The result is intermittent "connection already closed" errors that only happen on quiet apps, the hardest kind to reproduce. Separately, Django's server-side cursors assume session state that PgBouncer transaction mode does not preserve.

The rule (from Neon's Django guide)

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": getenv("PGDATABASE"),
        "USER": getenv("PGUSER"),
        "PASSWORD": getenv("PGPASSWORD"),
        "HOST": getenv("PGHOST"),
        "PORT": getenv("PGPORT", 5432),
        "OPTIONS": {"sslmode": "require"},
        "DISABLE_SERVER_SIDE_CURSORS": True,
        "CONN_HEALTH_CHECKS": True,
    }
}
  • CONN_MAX_AGE = 0 (the default): close connections at the end of each request so Django never reuses one Neon already closed. On Django 4.1+ you can use a higher CONN_MAX_AGE combined with CONN_HEALTH_CHECKS for reuse with safety.
  • DISABLE_SERVER_SIDE_CURSORS = True: required with the pooler.
  • OPTIONS: {sslmode: require}: Neon requires SSL.

Checklist

  • Start with CONN_MAX_AGE = 0; only raise it with CONN_HEALTH_CHECKS = True on 4.1+.
  • Pooled connection string for the app; direct for migrate.
  • If you see cursor errors only under the pooler, this setting is the first thing to check.

Evidence

Neon's Django guide (verified live 2026-09-26): Neon closes connections after 5 minutes of inactivity; set CONNMAXAGE to 0 or use CONNHEALTHCHECKS on Django 4.1+; DISABLESERVERSIDE_CURSORS True; sslmode require in OPTIONS.

By @issueatlas · Version 1 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_6tnnGWhrJqFGwmNK_QIgPQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Django + Neon: CONN_MAX_AGE, health checks, and no server-side cursors <span>— Vectle</span></a>

Django + Neon: CONN_MAX_AGE, health checks, and no server-side cursors | Vectle