The Fix
Fixes #1069 by updating the connection failure logic to preserve all error messages for various attempts, rather than only showing the final attempt's message.
Based on closed psycopg/psycopg issue #1069 · PR/commit linked
@@ -95,29 +95,43 @@ def connect(
rv = None
attempts = conninfo_attempts(params)
+ connection_errors: list[tuple[e.Error, str]] = []
for attempt in attempts:
try:
DEBUG - psycopg - connection attempt failed: host: '***' port: '5432', hostaddr '54.158.101.233': connection failed: connection to server at "54.158.101.233", port 5432 failed: root certificate file "./root.pem" does not exist
Either provide the file, use the system's trusted roots with sslrootcert=system, or change sslmode to disable server certificate verification.
DEBUG - psycopg - connection attempt failed: host: '***' port: '5432', hostaddr '2600:1f18:692c:303:31c4:3b43:ed6d:d04d': connection is bad: connection to server at "2600:1f18:692c:303:31c4:3b43:ed6d:d04d", port 5432 failed: Network is unreachable
Is the server running on that host and accepting TCP/IP connections?
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Apply the official fix\nFixes #1069 by updating the connection failure logic to preserve all error messages for various attempts, rather than only showing the final attempt's message.\nWhen NOT to use: This fix is not suitable if the application requires strict error handling for each connection attempt.\n\n
Why This Fix Works in Production
- Trigger: OperationalError: the server doesn't like you
- Mechanism: The connection logic overwrites previous exceptions, suppressing relevant error messages from earlier attempts
- If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).
Why This Breaks in Prod
- The connection logic overwrites previous exceptions, suppressing relevant error messages from earlier attempts
- Surfaces as: OperationalError: the server doesn't like you
Proof / Evidence
- GitHub issue: #1069
- Fix PR: https://github.com/psycopg/psycopg/pull/1076
- 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.55
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“I can take a look tomorrow and see, it shouldn't be too big of a change I would think.”
“Thank you very much. For something similar in our codebase, the libpq import function does something similar: it tries until one of three possible attempts…”
“Thanks for the reference, this is very helpful. Should the async_to_sync.py conversion be done by me and checked in as part of the PR, or…”
“Thanks for the additional context around the error handling in the project, that definitely gives me a bit more perspective”
Failure Signature (Search String)
- OperationalError: the server doesn't like you
Error Message
Stack trace
Error Message
-------------
OperationalError: the server doesn't like you
Multiple connection attempts failed. Previous failures were:
- standby server didn't like you either
- TypeError: some internal bug bugged
Minimal Reproduction
DEBUG - psycopg - connection attempt failed: host: '***' port: '5432', hostaddr '54.158.101.233': connection failed: connection to server at "54.158.101.233", port 5432 failed: root certificate file "./root.pem" does not exist
Either provide the file, use the system's trusted roots with sslrootcert=system, or change sslmode to disable server certificate verification.
DEBUG - psycopg - connection attempt failed: host: '***' port: '5432', hostaddr '2600:1f18:692c:303:31c4:3b43:ed6d:d04d': connection is bad: connection to server at "2600:1f18:692c:303:31c4:3b43:ed6d:d04d", port 5432 failed: Network is unreachable
Is the server running on that host and accepting TCP/IP connections?
What Broke
Users faced confusion due to misleading error messages when connection attempts failed.
Why It Broke
The connection logic overwrites previous exceptions, suppressing relevant error messages from earlier attempts
Fix Options (Details)
Option A — Apply the official fix
Fixes #1069 by updating the connection failure logic to preserve all error messages for various attempts, rather than only showing the final attempt's message.
Fix reference: https://github.com/psycopg/psycopg/pull/1076
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix is not suitable if the application requires strict error handling for each connection attempt.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Add a TLS smoke test that performs a real handshake in CI (include CA bundle validation and hostname checks).
- Alert on handshake failures by error string and endpoint to catch cert/CA changes quickly.
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.