Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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");
    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



  • Hi and welcome to devnet,

    are you sure dbHost is set correctly?



  • I've set dbHost like this:

    const QString dbHost = "192.168.178.26";
    


  • 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?