Unsupported versions: 7.0 / 6.5
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

Chapter 47. libpq C++ Binding

libpq++ is the C++ API to Postgres. libpq++ is a set of classes which allow client programs to connect to the Postgres backend server. These connections come in two forms: a Database Class and a Large Object class.

The Database Class is intended for manipulating a database. You can send all sorts of SQL queries to the Postgres backend server and retrieve the responses of the server.

The Large Object Class is intended for manipulating a large object in a database. Although a Large Object instance can send normal queries to the Postgres backend server it is only intended for simple queries that do not return any data. A large object should be seen as a file stream. In the future it should behave much like the C++ file streams cin, cout and cerr.

This chapter is based on the documentation for the libpq C library. Three short programs are listed at the end of this section as examples of libpq++ programming (though not necessarily of good programming). There are several examples of libpq++ applications in src/libpq++/examples, including the source code for the three examples in this chapter.

Control and Initialization

Environment Variables

The following environment variables can be used to set up default values for an environment and to avoid hard-coding database names into an application program:

Note: Refer to the libpq for a complete list of available connection options.

The following environment variables can be used to select default connection parameter values, which will be used by PQconnectdb or PQsetdbLogin if no value is directly specified by the calling code. These are useful to avoid hard-coding database names into simple application programs.

Note: libpq++ uses only environment variables or PQconnectdb conninfo style strings.

  • PGHOST sets the default server name. If a non-zero-length string is specified, TCP/IP communication is used. Without a host name, libpq will connect using a local Unix domain socket.

  • PGPORT sets the default port or local Unix domain socket file extension for communicating with the Postgres backend.

  • PGDATABASE sets the default Postgres database name.

  • PGUSER sets the username used to connect to the database and for authentication.

  • PGPASSWORD sets the password used if the backend demands password authentication.

  • PGREALM sets the Kerberos realm to use with Postgres, if it is different from the local realm. If PGREALM is set, Postgres applications will attempt authentication with servers for this realm and use separate ticket files to avoid conflicts with local ticket files. This environment variable is only used if Kerberos authentication is selected by the backend.

  • PGOPTIONS sets additional runtime options for the Postgres backend.

  • PGTTY sets the file or tty on which debugging messages from the backend server are displayed.

The following environment variables can be used to specify user-level default behavior for every Postgres session:

  • PGDATESTYLE sets the default style of date/time representation.

  • PGTZ sets the default time zone.

The following environment variables can be used to specify default internal behavior for every Postgres session:

  • PGGEQO sets the default mode for the genetic optimizer.

  • PGRPLANS sets the default mode to allow or disable right-sided plans in the optimizer.

  • PGCOSTHEAP sets the default cost for heap searches for the optimizer.

  • PGCOSTINDEX sets the default cost for indexed searches for the optimizer.

Refer to the SET SQL command for information on correct values for these environment variables.