#include #include #include int main(int argc, char **argv) { // create what we need to connect: the db connection structure and its options PGconn *conn; const char *dbopts; // accept an argument on the command line to pass to Postgres if (argc > 1) { dbopts = argv[1]; } else { dbopts = "dbname = default"; } // 1: attempt to connect conn = PQconnectdb(dbopts); // 2: check for an OK status that is... // 3: wrapped in a code block if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection failed: %s\n", PQerrorMessage(conn)); } else { // 4: do your business printf("Connection successful!\n"); } // 5: close the connection PQfinish(conn); exit(1); }