I can't access database from C++
every time it just throws error: FATAL: Ident authentication failed for user "testuser"
I had tried:
Reinstalling postgresql11 to postgresql12
Creating user
Creating database with owner testuser
Changing pg_hba.conf localhost from peer to md5
Changing the password of user multiple times
Rebooting computer
Restarting Service
here is the code:
#include "gtest/gtest.h"
#include <iostream>
#include <pqxx/pqxx>
using std::cin;
using std::cout;
using std::cerr;
using namespace std;
using namespace pqxx;
int main() {
try {
connection C("dbname = relay user = testuser password = 1234 hostaddr = 127.0.0.1 port = 5432");
if (C.is_open())
cout << "Opened database successfully: " << C.dbname() << endl;
else {
cout << "Can't open database" << endl;
return 1;
}
C.close();
} catch (const std::exception &e) {
cerr << e.what() << std::endl;
return 1;
}
}
TEST(postgreDB_test_trivial, trivial_test) {
main();
}
I allowed even firewall access for ports
I don't have any more ideas on how to resolve this.
The problem was that uninstalling Postgres11 and installing Postgres12 left pg_hba.conf with different setting after I restarted service so I needed to change everything to trust in pg_hba.conf to get it to work
Related
I am new to c++ and tring to send a mail using gmail smtp. I have done this in python but I am unable to do it in c++. I think I need to add startTLS() but it seems to be not defined. Please help me, I have tried my best. Searched the internet also could not find any thing.
#include <cstdlib>
#include <iostream>
#include <Poco/Net/SMTPClientSession.h>
#include <Poco/Net/MailMessage.h>
#include <string>
using namespace Poco::Net;
using namespace std;
int main(int argc, char** argv)
{
std::string Username = "farhantestingsmtp#gmail.com";
std::string Password = "cpguxktxoyibiybd";
std::string Receiver = "jattfarhan10#gmail.com";
std::string Name = "Jatt Farhan";
std::string Subject = "Hello!";
std::string Content = "TEXT OF THE EMAIL";
try
{
MailMessage msg;
msg.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, Receiver, Name));
msg.setSender(Username);
msg.setSubject(Subject);
msg.setContent(Content);
SMTPClientSession smtp("smtp.gmail.com", 487);
smtp.open();
cout << "Before Login";
smtp.login(SMTPClientSession::AUTH_LOGIN, Username, Password);
cout << "After Login";
smtp.sendMessage(msg);
smtp.close();
std::cerr << "Sent mail successfully!" << std::endl;
}
catch (std::exception& e)
{
std::cerr << "Failed to send mail: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
I have added my mail username and password, as it for testing. Thanks in advance.
I tried to add startTLS and could not do it as The SecureSMTPClientSession was not defined in my VS environment. I also searched the internet, but could not fix it.
I wrote an example application in C++(on Eclipse Luna) using sqlapi++ libraries to connect to an Oracle database I created with sqldeveloper.
The program compiles without errors, but when I run it,nothing appears on the console.
(I am using Windows 7)
Here the Database information:
Database name:"DB Casa Editrice"
Host:localhost
SID:xe
Port:1521
And the code:
#include <iostream>
#include "SQLAPI.h"
using namespace std;
int main() {
SAConnection con; // create connection object
try {
con.Connect( "DB Casa Editrice", // database name
"system", // user name
"shruikan94", // password
SA_Oracle_Client );
cout << "We are connected!\n";
con.Disconnect();
cout << "Disconnected!\n";
}
catch( SAException &x )
{
// SAConnection::Rollback()
// can also throw an exception
// (if a network error for example),
// we will be ready
try {
// on error rollback changes
con.Rollback();
}
catch( SAException & )
{
}
// print error message
cout << "ERROR\n";
}
return 0;
}
Another question on a Mysql connection failing:
I'm using the Poco library (1.5.2) and I would like to know why, when I try to open a MySQL connection, I got this message:
Connection attempt failed
Whereas, when I try a connection via console (mysql -u root -p ...), it works.
Maybe I forget an important step in the MySQL configuration ?
Here is my code :
#include <iostream>
#include <string>
#include <Poco/Data/MySQL/MySQLException.h>
#include <Poco/Data/MySQL/Connector.h>
#include <Poco/Data/SessionFactory.h>
using namespace std;
int main()
{
Poco::Data::MySQL::Connector::registerConnector();
try
{
string str = "host=localhost;user=root;password=mypassword;compress=true;auto-reconnect=true";
Poco::Data::Session test(Poco::Data::SessionFactory::instance().create(Poco::Data::MySQL::Connector::KEY, str ));
}
catch (Poco::Data::MySQL::ConnectionException& e)
{
cout << e.what() << endl;
return -1;
}
catch(Poco::Data::MySQL::StatementException& e)
{
cout << e.what() << endl;
return -1;
}
return 0;
}
Thank you !!
ok the problem was the "localhost" value for "host" doesn't work on my linux (I don't know why). For fixing the bug, I had to change my string to:
string str = "host=127.0.0.1;user=root;password=mypassword;compress=true;auto-reconnect=true";
Just another problem with Soci... I want to connect with my testDB which I've just created but the code below shows fatal error.
I did this:
On PostgreSQL:
1) CREATE USER robert WITH ENCRYPTED PASSWORD 'pass';
2) CREATE DATABASE testDB;
3) GRANT ALL PRIVILEGES ON DATABASE testDB TO robert;
My C++ code:
#include <iostream>
#include <soci.h>
#include <string>
using std::string;
using std::cout;
#include <postgresql/soci-postgresql.h>
bool connectToDatabase(string databaseName, string user, string password)
{
try
{
soci::session sql(soci::postgresql, "dbname=" +databaseName + " user="+user + " password="+password);
}
catch (soci::postgresql_soci_error const & e)
{
std::cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
return false;
}
catch (std::exception const & e)
{
std::cerr << "Some other error: " << e.what() << std::endl;
return false;
}
return true;
}
int main(int argc, char **argv)
{
cout << connectToDatabase("testDB", "robert", "pass");
return 0;
}
after compilation I got this:
Some other error: Cannot establish connection to the database.
FATAL: database "testDB" does not exist
What's wrong?
Connect to database "testdb" instead.
Case folding in PostgreSQL works by converting unquoted identifiers to lower case. So CREATE DATABASE testDB creates a database called "testdb", in contrast to CREATE DATABASE "testDB".
(As a general piece of advice: either get used to using all-lowercase identifiers, maybe with underscores between words, or get used to quoting identifiers all the time. The former is more natural for PostgreSQL, the latter allows you to stick with your convention despite it).
I am using mysql library to connect to my database (mysql) to retrieve the data after connecting. Checked that my services are running properly.
Following is the part of code that does the connecting task..
// Specify our connection target and credentials
const string server = "tcp://127.0.0.1:3306";
const string username = "root";
const string password = "";// No password - thanks, WAMP Server!
// Try to get a driver to use to connect to our DBMS
try {
driver = get_driver_instance();
if( driver == NULL )
throw SQLException("Null driver instance returned.");
}
catch (SQLException e) {
cout << "Could not get a database driver. Error message: " << e.what() << endl;
return -3;
}
// Try to connect to the DBMS server
try {
dbConn = driver->connect(server, username, password);
}
catch (sql::SQLException e) {
cout << "Could not connect to database. Error message: " << e.getSQLStateCStr() << " Error Code: " << e.getErrorCode() << endl;
cout << "Could not connect to database. Error: " << e.what() << endl;
return -1;
}
It compiles well but gives an unknown exception with unknown debug info. Something like this. Please help.
This worked after changing the string to SQLString for all the connection params like server, username and password.
This worked out like a charm.