Context
I’ve just installed postgres 9.1.4 on Lion, and i would like to write a simple client in C (libpq) to connect to PG databases.
psql -l says that there is a database named mydb owned by postgresDB.
psql mydb says : mydb=#
Here the little program that tries to connect to mydb :
// Server coordinates
const char * keywords[] = {"dbname", "user", NULL};
const char * values[] = {"mydb", "postgresDB", NULL};
// Check server state
analyse_PQpingParams (PQpingParams (keywords, values, 0));
// Connect to the database
PGconn * conn;
conn = PQconnectdbParams (keywords, values, 0);
// Check if the connection is healthy
analyse_PQstatus (PQstatus (conn));
// Clean exit
PQfinish (conn);
exit (0);
Problem
Here what the program retuns :
analyse_PQpingParams says :
------
The server is running and appears to be accepting connections.
------
analyse_PQstatus says :
------
The state returned was NULL.
------
Why is the conn object NULL ?
What the documentation says :
Note that these functions will always return a non-null object pointer, unless perhaps there is too little memory even to allocate the PGconn object. If it was a memory problem, then psql could not have connected to my db. So, the documentation doesn’t help here. Maybe elsewhere ?
Many thanks for your answers !
Pierre.
analyse_PQstatus code :
void analyse_PQstatus (int status)
{
cout << "\nanalyse_PQstatus says :" << endl;
cout << "------" << endl;
switch (status)
{
case CONNECTION_STARTED:
cout << "Waiting for connection to be made." << endl;
break;
case CONNECTION_MADE:
cout << "Connection OK; waiting to send." << endl;
break;
case CONNECTION_AWAITING_RESPONSE:
cout << "Waiting for a response from the server." << endl;
break;
case CONNECTION_AUTH_OK:
cout << "Received authentication; waiting for backend start-up to finish." << endl;
break;
case CONNECTION_SSL_STARTUP:
cout << "Negotiating SSL encryption." << endl;
break;
case CONNECTION_SETENV:
cout << "Negotiating environment-driven parameter settings." << endl;
break;
case 0 :
cout << "The state returned was NULL" << endl;
break;
default :
cout << "could not analyse this status code : " << status << endl;
}
cout << "------\n" << endl;
}
Can you show the
analyse_PQstatusfunction? Are you really finding thatconnis NULL or that the result ofPQstatus(conn)(which is what you are passing to the function) is 0, i.e.CONNECTION_OK?