Jump to solution
Verify

The Fix

Fixes the issue where OperationalError.pgconn is None when a password is needed by retaining pgconn on OperationalError.

Based on closed psycopg/psycopg issue #1246 · PR/commit linked

Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.

Jump to Verify Open PR/Commit
@@ -10,6 +10,13 @@ --------------- +Psyocpg 3.3.3 (unreleased) +^^^^^^^^^^^^^^^^^^^^^^^^^^ +
repro.py
import psycopg try: with psycopg.connect(): pass except psycopg.OperationalError as e: assert e.pgconn assert e.pgconn.needs_password
verify
Re-run: PGHOST=localhost PGPORT=5434 PGUSER=postgres python needs_password.py
fix.md
Option A — Apply the official fix\nFixes the issue where OperationalError.pgconn is None when a password is needed by retaining pgconn on OperationalError.\nWhen NOT to use: Do not use this fix if the application relies on the previous behavior of pgconn being None.\n\n

Why This Fix Works in Production

  • Trigger: AssertionError
  • Mechanism: The pgconn attribute is lost when raising OperationalError for multiple connection failures
Production impact:
  • If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.

Why This Breaks in Prod

  • The pgconn attribute is lost when raising OperationalError for multiple connection failures
  • Surfaces as: $ PGHOST=localhost PGPORT=5434 PGUSER=postgres python needs_password.py

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“I use OperationalError.pgconn.needs_password in a script to prompt the user for his password if needed. This no longer works as of 3.2.8 (specifically #1076) because attribute pgconn is always None. Here's my reproducer: Running that script”
Issue thread · issue description · source

Failure Signature (Search String)

  • AssertionError

Error Message

Stack trace
error.txt
Error Message ------------- $ PGHOST=localhost PGPORT=5434 PGUSER=postgres python needs_password.py Traceback (most recent call last): File "/home/ewie/src/psycopg-regress/needs_password.py", line 4, in <module> with psycopg.connect(): ~~~~~~~~~~~~~~~^^ File "/home/ewie/src/psycopg/psycopg/psycopg/connection.py", line 133, in connect raise enhanced_exception.with_traceback(None) psycopg.OperationalError: connection failed: connection to server at "127.0.0.1", port 5434 failed: fe_sendauth: no password supplied Multiple connection attempts failed. All failures were: - host: None, port: None, hostaddr: '::1': connection failed: connection to server at "::1", port 5434 failed: fe_sendauth: no password supplied - host: None, port: None, hostaddr: '127.0.0.1': connection failed: connection to server at "127.0.0.1", port 5434 failed: fe_sendauth: no password supplied During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/ewie/src/psycopg-regress/needs_password.py", line 7, in <module> assert e.pgconn ^^^^^^^^ AssertionError

Minimal Reproduction

repro.py
import psycopg try: with psycopg.connect(): pass except psycopg.OperationalError as e: assert e.pgconn assert e.pgconn.needs_password

What Broke

Users cannot prompt for passwords due to pgconn being None, leading to connection failures.

Why It Broke

The pgconn attribute is lost when raising OperationalError for multiple connection failures

Fix Options (Details)

Option A — Apply the official fix

Fixes the issue where OperationalError.pgconn is None when a password is needed by retaining pgconn on OperationalError.

When NOT to use: Do not use this fix if the application relies on the previous behavior of pgconn being None.

Fix reference: https://github.com/psycopg/psycopg/pull/1247

Last verified: 2026-02-09. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • Do not use this fix if the application relies on the previous behavior of pgconn being None.

Verify Fix

verify
Re-run: PGHOST=localhost PGPORT=5434 PGUSER=postgres python needs_password.py

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
  • Pin production dependencies and upgrade only with a reproducible test that hits the failing path.

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.