Connecting to PostgreSQL server with Qt5.4
-
Hey guys,
I want to create an application which uses a PostgreSQL database on a dedicated server.
I've installed the drivers and I'm about to initialize my connection like this:DBConnection = QSqlDatabase::addDatabase("QPSQL"); DBConnection.setHostName(dbHost); DBConnection.setDatabaseName(dbName); DBConnection.setUserName(dbUser); DBConnection.setPassword(dbPass); if(DBConnection.open()){ qDebug() << "Succesfully Connected"; } else { qDebug() << "Connection Error"; qDebug() << DBConnection.lastError().text(); }
The value of dbHost is the IP adress of the server.
If I run the programm, I get this output:Connection Error could not connect to server: File or directory not found Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
For me it looks like the driver wants to connect to localhost, even if I enter an IP adress as the host.
Is there a way to solve my problem, without writing a complete new driver?Greetings
Hendrik -
I've set dbHost like this:
const QString dbHost = "192.168.178.26";
-
I was able to remotely access Postgres database on a different PC.
This video was very helpful.
https://www.youtube.com/watch?v=4IhfbxhoAn8
Basically you need to open the windows firewall (or create an exception for the port) and change the line
host all all 127.0.0.1/32 scram-sha-256
to
host all all 0.0.0.0/0 scram-sha-256 in the pg_hba.conf file.