October 16th, 2013, 06:47 AM
[SOLVED] Libpq - Connection to database failed: timeout exired
I have PostgreSQL 9.3 (x86) installed, the server service is up (status = Started) and I can connect to it with pgAdmin.
On the server there is a database: postgres and it's owner is user "postgres" with password "admin". The server is running on localhost, port 5433.
Yet when I try to access my database from C++ code with the following code:
I get an error: "Connection to database failed: timeout exired"
static void exit_nicely(PGconn *conn)
int main(int argc, char **argv)
const char *conninfo;
conninfo = "host=localhost port=5433 dbname=postgres user=postgres password=admin connect_timeout=5";
fprintf(stdout, "# Start\n");
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK)
fprintf(stderr, "Connection to database failed: %s",
fprintf(stdout, "# Connected\n");
I'd be grateful for any clues what might cause such a behavior
October 16th, 2013, 12:55 PM
is your server listening on "localhost"? Can your application resolve that to the correct IP?
I trust that you are able you connect through the psql commandline?
October 16th, 2013, 01:30 PM
Hey Vinny, thank you for your response!
Originally Posted by Vinny42
I did some testing with psql and found out that the time needed to connect to the db on my system is ~30-40 sec. So all I had to do was to set connect_timeout parameter to let's say 60 and the problem is gone
Such a stupid thing..
October 16th, 2013, 01:36 PM
40 seconds to connect? Don't you mean 40 milliseconds?