C/C++ secure connection example?

I need to make some simple insert queries from a C process. So far my google foo has failed to find out how to make a connection with libpg/libpg++. Any chance of an example? If it is even possible.

Actually I’m finding the libpq++ documentation pretty impenetrable just now. Does anyone have a link to a nice simple tutorial or examples?

I believe libpq++ calls down to libpq, so you should be able to specify a number of ssl-related flags in the connection URI. Something along the lines of: sslmode=verify-full&sslcert=client.crt&sslkey=client.key&sslroot=ca.crt

Please see the official docs for more details.

Thanks marc,

After a bit of a struggle this secure connection from C++ worked:

#include <cassert>
#include <functional>
#include <iostream>
#include <stdexcept>
#include <string>
#include <pqxx/pqxx>

using namespace std;

int main() {
  try {
    // Connect to the "time_series" database.
    pqxx::connection c("postgresql://someone@crdb-1:26257/time_series?password=1234&sslmode=verify-full&sslrootcert=ca.crt");

    pqxx::nontransaction w(c);

    // Print out start of series_1.
    cout << "Initial series_1:" << endl;
    pqxx::result r = w.exec("SELECT * FROM series_1 LIMIT 10");
    for (auto row : r) {
      cout << row[0].as<int>() << ' ' << row[1].as<string>() << ' ' << row[2].as<int>() << endl;

    w.commit();  // Note this doesn't doesn't do anything
                 // for a nontransaction, but is still required.
  catch (const exception &e) {
    cerr << e.what() << endl;
    return 1;
  cout << "Success" << endl;
  return 0;

Grr… part of the reason that I was having a struggle is that this works:

pqxx::connection c("postgresql://root@crdb-1:26257/time_series?sslmode=verify-full&sslrootcert=ca.crt&sslcert=client.root.crt&sslkey=client.root.key");

But, if the client.root.crt and client.root.key files are not found the thing gives the following error:

fe_sendauth: no password supplied

Rather than indicating it cannot find the files.