문제

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 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).

Thanks.

도움이 되었습니까?

해결책

So after doing some more research and asking postgres developers it seems that it is not currently possible to get the error code from PQconnectdbParams().

This seems to be a shortcoming of libpq itself which doesn't try to assign SQLState error codes for errors it detects internally (like an unknown hostname).

This seems to be on the postgres dev todo list but it's not a priority.

TL;DR: If you are going to use libpq, learn to live with the text error message (whatever language it is localized in).

다른 팁

Try...

cout << PQerrorMessage(conn) << endl;

Try to call getpeername() on PQsocket(conn). In case of server being down getpeername return error, but in case of wrong password tcp connection was successfully established, and getpeername return server address.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top