Solved Unable to use PostgreSQL with Qt 5.2.1
-
Even after reading related topics in the forum, and the details given in http://qt-project.org/doc/qt-5/sql-driver.html I am still unable to get a simple Qt desktop dialog application to work with PostgreSQL in Windows 7 64-bit. I am able to compile the application, but when I run it (from within Qt Creator), it is not loading the database driver and I am getting the following error (please note that I have the environment variable QT_DEBUG_PLUGINS set to 1).
_QFactoryLoader::QFactoryLoader() looking at "C:/bin/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/plugins/sqldrivers/qsqlpsqld.dll"
Found metadata in lib C:/bin/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/plugins/sqldrivers/qsqlpsqld.dll, metadata=
{
"IID": "org.qt-project.Qt.QSqlDriverFactoryInterface",
"MetaData": {
"Keys": [
"QPSQL7",
"QPSQL"
]
},
"className": "QPSQLDriverPlugin",
"debug": true,
"version": 328193
}Got keys from plugin meta data ("QPSQL7", "QPSQL")
QFactoryLoader::QFactoryLoader() looking at "C:/bin/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/plugins/sqldrivers/qsqlpsqld.pdb"
"Plugin verification data mismatch in 'C:/bin/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/plugins/sqldrivers/qsqlpsqld.pdb'"
not a plugin
QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/testuser/qtwork/build-TestApp-Desktop_Qt_5_2_1_MSVC2012_OpenGL_64bit-Debug/debug/sqldrivers" ...
loaded library "C:/bin/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/plugins/sqldrivers/qsqlpsqld.dll"
QLibraryPrivate::loadPlugin failed on "C:/bin/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/plugins/sqldrivers/qsqlpsqld.dll" : "Cannot load library C:/bin/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/plugins/sqldrivers/qsqlpsqld.dll: "
QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
QFactoryLoader::QFactoryLoader() checking directory path "C:/bin/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/plugins/accessiblebridge" ... _The location of PostgreSQL library libpq.dll, C:\Program Files\PostgreSQL\9.3\bin, has been appended to the PATH variable.
This is what I am using:
(1) Qt Creater 3.0.1
(2) Qt 5.2.1 MSVC2012 OpenGL 64bit
(3) PostgreSQL 9.3 64-bit
(4) Windows 7 64-bit
(5) Microsoft Visual C++ Compiler 11.0The code snippet is quite simple, taken from the documentation pages.
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); //db.setHostName("localhost"); db.setDatabaseName("mydb"); db.setUserName("postgres"); db.setPassword("postgres"); bool ok = db.open();
if (ok == false) {
QSqlError error = db.lastError();
// printing the error here
}The following is my project file:
_#-------------------------------------------------Project created by QtCreator 2014-03-31T09:51:40
#-------------------------------------------------
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestApp
TEMPLATE = appSOURCES += main.cpp
dialog.cppHEADERS += dialog.h
FORMS += dialog.ui
LIBS += -LC:\Program Files\PostgreSQL\9.3\lib
LIBS += -llibpq_Any help or hint to resolve this would be highly appreciated.
-
Hi and welcome to devnet,
Check that the PATH environment variable in the Run part of the Project panel also contains the path to the PostgreSQL dll
Hope it helps
-
Hello SGalst, thank you for the reply.
Yes the path to PostgreSQL dll, C:\Program Files\PostgreSQL\9.3\bin, is already appended to the PATH env. variable in the Run part of the Project panel.
-
Solved after using Dependency Walker to look at qsqlpsqld.dll.
-
This also happens on 5.7.
I've created an issue and a resolution here:
https://bugreports.qt.io/browse/QTBUG-55077
TL;DR = Add the location of the LIBPQ.DLL client library to your windows PATH envar.