The libpq++ API provides five related classes that you can use to build client applications written in C++:

  • PgConnection? Provides a minimal object that can manage a database connection.

  • PgDatabase? Extends PgConnection with a set of member functions that provide access to a result set.

  • PgTransaction? Extends PgDatabase to provide an automatic transaction context.

  • PgCursor? Extends PgTransaction with member functions that manage a cursor.

  • PgLargeObject? Extends PgConnection with member functions for dealing with large-objects.

You are most likely to use the PgDatabase and PgLargeObject classes in your applications. You might extend the PgConnection class if you want to control the connection process more closely. I recommend that you view the PgTransaction and PgCursor classes as sample code. You probably won't use either of those classes, but you can certainly learn from their implementations.

libpq++ is a wrapper around the libpq C API. If you build your own C++ classes based on PgConnection, you can use the entire libpq API because a PgConnection object contains a PGconn * (and you need a PGConn * to use the libpq API).

At the time I am writing this chapter, another C++ API is appearing in the PostgreSQL community. The libpqxx library is a STL-friendly class library that seems to be much more complete than libpq++. libpqxx provides individual classes for dealing with database connections, transactions, cursors, triggers, and result sets. The classes exported by libpqxx provide a number of convenience features. For example, you can iterate through a result set using array indexing (rather than using explicit member functions).

You can find more information about libpqxx at By the time this book is published, libpqxx may be an official part of the PostgreSQL distribution.

    Part II: Programming with PostgreSQL