Client 1—Connecting to the Server

Connecting to a database using libpgeasy is simple. libpgeasy provides a single connection function:

PGconn * connectdb( char * options );

The single argument to connectdb() is a connection string in the same form expected by the libpq PQconnectdb() function. An example connection string might look like this:

char * connectString = "dbname=movies user=sheila";

Let's look at a simple client that uses the connectdb() function:

/* client1.c */

#include <stdlib.h>
#include <libpq-fe.h>
#include <libpgeasy.h>

int main( int argc, char * argv[] )
{
  connectdb( argv[1] ? argv[1] : "" );
  disconnectdb();
  exit( EXIT_SUCCESS );

}

You can use the following makefile to compile and link all samples in this chapter (see ...

Get PostgreSQL, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.