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

0

I try to make GUI C++ 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:

TablePlus

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?

Thank you.

c++
postgresql
qt
ssh
asked on Stack Overflow Jan 9, 2020 by marcus74 • edited Jun 20, 2020 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0