March 8th, 2017, 05:40 PM
How do I get pqxx (the C++ API for Postgres) to work with database authentication?
How do I get pqxx (the C++ API for Postgres) to work when my database authentication requires a password?
I installed C++, Postgres, and postgresql-devel on CentOS 7.3. I installed libpqxx-4.0 too.
I then created this add_employee.cxx file and based it on the "brief example" here.
I compiled it with this:
int main(int, char *argv)
pqxx::connection c("dbname=foobar user=jdoe password=smartpassword");
pqxx::result r = txn.exec(
"SELECT id "
"FROM Employee "
"WHERE name =" + txn.quote(argv));
if (r.size() != 1)
<< "Expected 1 employee with name " << argv << ", "
<< "but found " << r.size() << std::endl;
int employee_id = r.as<int>();
std::cout << "Updating employee #" << employee_id << std::endl;
"UPDATE EMPLOYEE "
"SET salary = salary + 1 "
"WHERE id = " + txn.quote(employee_id));
I ran ./a.out and saw this:
c++ add_employee.cxx -lpqxx -lpq
I normally need a password for the database role "jdoe" to log in. What do I do to get around the error? I want to have a C++ program log in automatically. I do not have passwordless authentication into the database. Is there a way to have this C++ program log into the Postgres database despite requiring a password?
terminate called after throwing an instance of 'pqxx::broken_connection' what(): FATAL: Peer authentication failed for user "foobar"
March 8th, 2017, 06:24 PM
Re: How do I get pqxx (the C++ API for Postgres) to work with database authentication
The connection string looks like it has the correct parameters. Perhaps you could try to add the host, e.g., "host=localhost dbname=foobar user=jdoe password=smartpassword" (or equivalently, "postgresql://jdoe:smartpassword@localhost/foobar") and double check that the password is correct.
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!