Getting the SQLSTATE after a failed connection

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Getting the SQLSTATE after a failed connection
Date: 2025-10-17 14:26:44
Message-ID: CA+mi_8ZRi-NJsOw5SFUMXACLm-OFoS+DeRV3_5m7RgHfPXx1gg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello,

in ticket <https://github.com/psycopg/psycopg/issues/1188> an user
showed that, upon a failed connection attempt because of a bad
password, the server sends a SQLSTATE 28P01 message. However psycopg
cannot raise the expected `InvalidPassword` exception, because the
only way to get the SQLSTATE seems to be from a PGresult via
PQresultErrorField and I fail to see any API call to get the info from
a PGconn.

Using `PQmakeEmptyPGresult` didn't help either: PQresultErrorField
reports no data about the error.

Browsing the code, it seems that PqMsg_CommandComplete is only handled
in isBusy/getResult. There is a promising `last_sqlstate` in the
structure but it seems only used internally and not exposed.

Is there any way to get the last SQLSTATE from the connection object
or is that information lost?

Thank you very much

-- Daniele

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2025-10-17 14:28:17 Re: Fix lag columns in pg_stat_replication not advancing when replay LSN stalls
Previous Message Nathan Bossart 2025-10-17 14:23:15 Re: anonymous unions (C11)