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%.
@@ -10,6 +10,13 @@
---------------
+Psyocpg 3.3.3 (unreleased)
+^^^^^^^^^^^^^^^^^^^^^^^^^^
+
import psycopg
try:
with psycopg.connect():
pass
except psycopg.OperationalError as e:
assert e.pgconn
assert e.pgconn.needs_password
Re-run: PGHOST=localhost PGPORT=5434 PGUSER=postgres python needs_password.py
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
- 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
- GitHub issue: #1246
- Fix PR: https://github.com/psycopg/psycopg/pull/1247
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.70
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.46
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”
Failure Signature (Search String)
- AssertionError
Error Message
Stack trace
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
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.
Fix reference: https://github.com/psycopg/psycopg/pull/1247
Last verified: 2026-02-09. Validate in your environment.
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
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.