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. Connect with database standing on host which requires SSH connection using Qt
QtWS25 Last Chance

Connect with database standing on host which requires SSH connection using Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 2.1k Views
  • 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.
  • M Offline
    M Offline
    marcus74
    wrote on last edited by
    #1

    I try to make application with PostgreSQL database. It is located on remote server which is not visible in public but requires establishment of tunnel over SSH. When I want to make a connection in TablePlus, configuration looks like:
    938ab5c3-77cd-4535-b2f3-fff4fe0b4868-obraz.png
    and it works perfectly. But in QtCreator I have following code which generates error:

    #include "mainwindow.h"
    #include <QApplication>
    #include <QSqlDatabase>
    #include <QDebug>
    #include "QSqlError"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    
        db.setHostName("host.with.database.com");
        db.setDatabaseName("database");
        db.setPassword("******");
        db.setUserName("userFromAboveHost");
    
        if(db.open())
        {
            qDebug() <<"opened" ;
            db.close();
        }
        else
        {
            qDebug() << db.lastError().text();
            exit(1);
        }
    
        MainWindow w;
        w.show();
    
        return a.exec();
    
    }
    
    "could not connect to server: Connection timed out (0x0000274C/10060)
    Is the server running on host "host.with.database.com" and accepting TCP/IP connections on port 5432?
    QPSQL: Unable to connect"
    

    To inform you in advance, host accepts such kind of connections because connection in TablePlus is possible without obstacles.

    My questions are: What should I add to my code? Is it overally possible to establish that connection using Qt? Or am I forced to change the technology?

    I tried:

    QProcess *proc = new QProcess();
    proc->start("ssh -L 5433:localhost:5432 userFromAbove@host.with.database.com");
    

    and

    QTcpSocket sock;
    sock.setProxy(QNetworkProxy::NoProxy);
    sock.connectToHost("userFromAboveServer@host.with.database.com", 5433);
    

    but none of them works. Please help :) Thanks.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @marcus74 said in Connect with database standing on host which requires SSH connection using Qt:

      Is it overally possible to establish that connection using Qt?

      afaik it's not possible

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #3

        It's possible (I do it with a remote (only accessible with ssh) Microsoft's SQLServer)

        Establish the tunnel using ssh, preferably with 5432 on both sides, e.g.
        ssh -L 5432:localhost:5432 userFromAbove@host.with.database.com

        then start a QSqlDatabase connected to localhost:5432

        M 1 Reply Last reply
        3
        • hskoglundH hskoglund

          It's possible (I do it with a remote (only accessible with ssh) Microsoft's SQLServer)

          Establish the tunnel using ssh, preferably with 5432 on both sides, e.g.
          ssh -L 5432:localhost:5432 userFromAbove@host.with.database.com

          then start a QSqlDatabase connected to localhost:5432

          M Offline
          M Offline
          marcus74
          wrote on last edited by
          #4

          @hskoglund Thanks, seems like connection was created, but now I receive new error

          psql: FATAL:  Ident authentication failed for user "userFromAboveHost"
          

          despite of passing valid credentials. How to avoid that?

          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #5

            Hmm in the screenshot above it's ""userFromAboveServer" not ""userFromAboveHost"?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              marcus74
              wrote on last edited by
              #6

              @hskoglund Oh I see, it argues with postgres on my local machine and when I set hostname to "localhost", it tries to connect local database rather than remote. In fact

              ssh -L 5432:localhost:5432 userFromAboveHost@host.with.database.com
              

              seems to have no effect.

              fedbdb01-9edb-4d95-a43b-6978c2a657c8-obraz.png

              Generally, db tries to establish connection with local machine postgres all the time. Local postgres listen on 5432. Any ideas how to solve that? I think that I'm going to achieve the goal but it's still far away... :(

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @marcus74 said in Connect with database standing on host which requires SSH connection using Qt:

                Any ideas how to solve that?

                Use another port - you can't open a ssh tunnel on a port which is already in use.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                2
                • M Offline
                  M Offline
                  marcus74
                  wrote on last edited by
                  #8

                  Still does not work.

                  7fb6cd55-d396-4070-8514-e252aec6df47-obraz.png

                  ssh -L 5000:localhost:22 userFromAboveHost@host.with.database.com
                  

                  have no effect too.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    @Christian-Ehrlicher suggested to change the port on your local host side to avoid the clash no the two. You can also disable temporarily your local PostgreSQL during the testing.

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

                    M 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      Hi,

                      @Christian-Ehrlicher suggested to change the port on your local host side to avoid the clash no the two. You can also disable temporarily your local PostgreSQL during the testing.

                      M Offline
                      M Offline
                      marcus74
                      wrote on last edited by
                      #10

                      @SGaist I've done it on screen and it didn't work as well

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @marcus74 said in Connect with database standing on host which requires SSH connection using Qt:

                        I've done it on screen and it didn't work as well
                        ssh -L 5000:localhost:22 userFromAboveHost@host.with.database.com

                        No, you're now connecting to port 22 on your database server

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        M 1 Reply Last reply
                        0
                        • hskoglundH Offline
                          hskoglundH Offline
                          hskoglund
                          wrote on last edited by
                          #12

                          The procedure is pretty good (and officially) documented by PostgreSQL

                          1 Reply Last reply
                          3
                          • Christian EhrlicherC Christian Ehrlicher

                            @marcus74 said in Connect with database standing on host which requires SSH connection using Qt:

                            I've done it on screen and it didn't work as well
                            ssh -L 5000:localhost:22 userFromAboveHost@host.with.database.com

                            No, you're now connecting to port 22 on your database server

                            M Offline
                            M Offline
                            marcus74
                            wrote on last edited by
                            #13

                            @Christian-Ehrlicher On screen code I tried in second line:

                            ssh -L 5000:localhost:5432 userFromAboveHost@host.with.database.com
                            

                            and it gave above output.

                            1 Reply Last reply
                            0
                            • hskoglundH Offline
                              hskoglundH Offline
                              hskoglund
                              wrote on last edited by hskoglund
                              #14

                              Again hmm,, in the screenshot above you're using server.using.tunnel.com (not ```host.with.database.com``)?

                              Edit: I mean you should try this SSH:

                              ssh -L 5000:host.with.database.com:5432 userFromAboveServer@server.using.tunnel.com
                              
                              M 1 Reply Last reply
                              1
                              • hskoglundH hskoglund

                                Again hmm,, in the screenshot above you're using server.using.tunnel.com (not ```host.with.database.com``)?

                                Edit: I mean you should try this SSH:

                                ssh -L 5000:host.with.database.com:5432 userFromAboveServer@server.using.tunnel.com
                                
                                M Offline
                                M Offline
                                marcus74
                                wrote on last edited by
                                #15

                                @hskoglund Again, I receive errors about not accepting TCP/IP connections. Maybe I should pass by some way a password in process object for

                                userFromAboveServer@server.using.tunnel.com
                                

                                because in Windows cmd I am requested for password after doing this:

                                ssh -L 5000:host.with.database.com:5432 userFromAboveServer@server.using.tunnel.com
                                
                                1 Reply Last reply
                                0
                                • hskoglundH Offline
                                  hskoglundH Offline
                                  hskoglund
                                  wrote on last edited by
                                  #16

                                  Yeah, if you're establishing the SSH tunnel via a command line, then there's no stable way to submit the password.
                                  I think putty can be used in an automatic way, or, if you have a admin rights on server.using.tunnel.com you could create keys, so that no password is needed, more for example here

                                  1 Reply Last reply
                                  1
                                  • M Offline
                                    M Offline
                                    marcus74
                                    wrote on last edited by marcus74
                                    #17

                                    Ok, after long perturbations I used program:
                                    https://geekflare.com/create-port-listener-in-windows-or-linux/
                                    and created listening port 5000, then ran Qt project and everything is fine.
                                    Thanks for all your patience :)

                                    To conclude, passing passoword as I mentioned above is not mandatory.

                                    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