Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connecting to PostgreSQL server with Qt5.4
Forum Updated to NodeBB v4.3 + New Features

Connecting to PostgreSQL server with Qt5.4

Scheduled Pinned Locked Moved General and Desktop
postgresqldatabaseqt5.4
7 Posts 5 Posters 3.4k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hendrik4.10
    wrote on 10 May 2015, 17:03 last edited by
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 10 May 2015, 17:20 last edited by
      #2

      Hi and welcome to devnet,

      are you sure dbHost is set correctly?

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hendrik4.10
        wrote on 10 May 2015, 17:25 last edited by
        #3

        I've set dbHost like this:

        const QString dbHost = "192.168.178.26";
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on 10 May 2015, 17:49 last edited by
          #4

          Weird,

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

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 10 May 2015, 20:38 last edited by
            #5

            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 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • C Offline
              C Offline
              clochydd
              wrote on 11 May 2015, 08:53 last edited by
              #6

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

              1 Reply Last reply
              0
              • S Offline
                S Offline
                saim
                wrote on 27 May 2021, 13:34 last edited by
                #7

                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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved