Important: Please read the Qt Code of Conduct -

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");
        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?


  • Hi and welcome to devnet,

    are you sure dbHost is set correctly?

  • I've set dbHost like this:

    const QString dbHost = "";

  • Weird,

    can you debug the code and verify the value of variables?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    To add to @mcosta, and maybe a silly question bug are you sure that you are using the corrected address and not the one from own computer ?

  • Hi, to check the connection: can you access your database with the same parameters (host, port etc.) via pgAdmin?

  • I was able to remotely access Postgres database on a different PC.
    This video was very helpful.
    Basically you need to open the windows firewall (or create an exception for the port) and change the line
    host all all scram-sha-256
    host all all scram-sha-256 in the pg_hba.conf file.