2 * src/test/examples/testlibpq4.c
6 * this test program shows to use LIBPQ to make multiple backend
29 /* check to see that the backend connection was successfully made */
36 /* Set always-secure search path, so malicious users can't take control. */
38 "SELECT pg_catalog.set_config('search_path', '', false)");
49 main(
int argc,
char **argv)
65 * PGresult *res1, *res2;
71 fprintf(stderr,
"usage: %s tableName dbName1 dbName2\n", argv[0]);
72 fprintf(stderr,
" compares two tables in two databases\n");
81 * begin, by setting the parameters for a backend connection if the
82 * parameters are null, then the system will try to use reasonable
83 * defaults by looking up environment variables or, failing that, using
86 pghost = NULL;
/* host name of the backend */
87 pgport = NULL;
/* port of the backend */
88 pgoptions = NULL;
/* special options to start up the backend
91 /* make a connection to the database */
98 /* start a transaction block */
99 res1 =
PQexec(conn1,
"BEGIN");
102 fprintf(stderr,
"BEGIN command failed\n");
108 * make sure to PQclear() a PGresult whenever it is no longer needed to
114 * fetch instances from the pg_database, the system catalog of databases
116 res1 =
PQexec(conn1,
"DECLARE myportal CURSOR FOR select * from pg_database");
119 fprintf(stderr,
"DECLARE CURSOR command failed\n");
125 res1 =
PQexec(conn1,
"FETCH ALL in myportal");
128 fprintf(stderr,
"FETCH ALL command didn't return tuples properly\n");
133 /* first, print out the attribute names */
135 for (
i = 0;
i < nFields;
i++)
139 /* next, print out the instances */
142 for (
j = 0;
j < nFields;
j++)
149 /* close the portal */
150 res1 =
PQexec(conn1,
"CLOSE myportal");
153 /* end the transaction */
154 res1 =
PQexec(conn1,
"END");
157 /* close the connections to the database and cleanup */
#define fprintf(file, fmt, msg)
ConnStatusType PQstatus(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
PGresult * PQexec(PGconn *conn, const char *query)
#define PQsetdb(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME)
static const char * pghost
static const char * pgport
static const char * dbName
int main(int argc, char **argv)
static void exit_nicely(PGconn *conn1, PGconn *conn2)
static void check_prepare_conn(PGconn *conn, const char *dbName)