libpq: How to get the error code after a failed PGconn connection

Поиск
Список
Период
Сортировка
Искать

libpq: How to get the error code after a failed PGconn connection

От:
Hello World <worldanizer@gmail.com>
Дата:
Given the following code.

PGconn* const conn=PQconnectdbParams(keywords, values, false);
if(! conn || PQstatus(conn)!=CONNECTION_OK){ /* error code? */ }

- In case of a failed connection is there a way to get the error code to be able to distinguish between a (e.g.) bad password and the server being down.

(I know I can get the error message, but I want to be able to react to the cause of the error according to its cause, plus the error message is localized so I can't even scan that for keywords such as "permission denied").

ps. I've looked at how psql does it, and it seems it just prints the error message and exists.

ps. I've tried to take a look at the source but it seems it just sets the status to CONNECTION_BAD no matter the cause of error, then sets a specific error message.

Any help appreciated.

Thanks.

Re: libpq: How to get the error code after a failed PGconn connection

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Hello World  writes:
> Given the following code.
> PGconn* const conn=PQconnectdbParams(keywords, values, false);
> if(! conn || PQstatus(conn)!=CONNECTION_OK){ /* error code? */ }

> - In case of a failed connection is there a way to get the error code to be
> able to distinguish between a (e.g.) bad password and the server being down.

1. This question is not really material for the -hackers list.

2. No, I'm afraid.  libpq does not currently assign SQLSTATE error codes
to errors it detects internally, so even if there were an API for this,
it would fail to return anything in a lot of cases.  Fixing that is on
the TODO list, but it's been there for a long time, so don't hold your
breath ...
		regards, tom lane


FAQ