Setting up a connection

The very first thing to do before we can begin working with a database, is actually connect to one. We do this by creating a connection object that will serve as our "handle" on the connection:

	connection Conn("postgresql://localhost/test");
      

This gives us a connection object called Conn.

The connection constructor here takes one argument, the "connect string." The connection string may specify which host on the network runs the database backend we wish to connect to; which database we're interested in; which user name we'll be using to log in; etc. Refer to the libpq connect call for a complete definition of what may go into the connect string. In the example we're connecting to a database test residing on the local machine. By default the client will try to connect to a server running on the local machine.

The connection can now act as a "service counter" for our database; your client will use it to perform one or more transactions related to the database.

You can't copy or assign connections, with the exception of move construction or move assignment. You can move a connection object, but there are things you can do with the connection that make moving it impossible. For instance, once you've registered an error handler on your connection, any attempt to move the the connection will produce an exception.

Caution

As a matter of design, libpqxx will not allow you to perform queries on the connection directly. You will need to open a transaction instead.

See the section on transactions below.