O
O
odexed2018-04-25 13:05:51
PostgreSQL
odexed, 2018-04-25 13:05:51

How to build PostgreSQL driver for Qt under Windows 10?

It is necessary to create a minimally working Qt application for connecting and interacting with the PostgreSQL DBMS. Doesn't work out of the box. OS - windows 10 64bit, Qt version - Qt 5.9.5 MSVC 2015, MS Visual Studio 2017 Community, PostgreSQL 9.6 (tried both 32bit and 64bit) I
check with this code:

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    qDebug() << db.isValid();
    db.setHostName("hostname");
    db.setPort(5432);
    db.setDatabaseName("dbname");
    db.setUserName("username");
    db.setPassword("password");
    if (!db.open())
    {
        qDebug() << db.lastError().text();
        return;
    }

As a result:
QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
false

At the same time, there is a qsqlpsql.dll file in the Qt subdirectory msvc2017_64\plugins\sqldrivers. From which I concluded that the driver must be compiled manually.
I open the command line Qt 5.9.5 64bit for Desktop (MSVC 2017), go to the folder C:\Qt\Qt5.9.5\5.9.5\Src\qtbase\src\plugins\sqldrivers\psql, type the command
qmake "INCLUDEPATH+=\"C:\Program Files (x86)\PostgreSQL\9.6\include\"" "LIBS+=\"C:\Program Files (x86)\PostgreSQL\9.6\lib\libpq.lib\"" psql .pro

As a result:
Project ERROR: Library 'psql' is not defined.

I also tried to build using Qt Creator
Project ERROR: Library 'psql' is not defined.
Cannot read /qtsqldrivers-config.pri: No such file or directory
Project ERROR: Library 'psql' is not defined.

I also tried to collect the entire sqldrivers branch, similarly:
Project ERROR: Library 'psql' is not defined.

Whether there is generally an opportunity to collect any working sheaf?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
odexed, 2018-04-25
@odexed

It turned out to solve the problem with the help of Dependence Walker. Indeed, the problem was solved not by compilation, but by copying the necessary dlls to the folder with the program. Specifically, I copied libpq.dll, libeay32.dll, ssleay32.dll, libintl-8.dll, libiconv-2.dll, msvcr120.dll there, and the sqldrivers folder with dll from C:\Qt\Qt5.9.5\5.9. 5\msvc2017_64\plugins\sqldrivers. I only made sure that the bit depth matches, and everything worked.

V
vreitech, 2018-04-25
@fzfx

the conclusion is incorrect. you don't need to compile the driver. you should include it in your code (but this is not accurate).
well, you must also have at least the postgresql client installed, and its directory with binaries must be included in the system PATH.

K
Kastuś, 2018-04-25
@Gytim

I noticed this if an older postgrey was installed earlier. I decided by cleaning the system paths, somewhere in the properties of the computer was.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question