Qt6.8 error with Posgres DB connection: "QSqlDatabase: QPSQL driver not loaded"
-
hi everyone, I already know that are releated topics about this problem but I dont understand how troubleshoot the problem bised the suggestion to copy manually the ".dll" to the executable file location.
Premise
I'm using Sql to connect to a postgres DB for a month now and untill today, I had never got problems.
What I Tryed
- Update Qt6.7.2 to the latest version of Qt (6.8.x)
- Uninstall Postgres 17.1 and install Postgres latest version (17.2)
- In VisualStudioCode I tryed to connect to the database with a query and retrive one of the table's data and works fine.
- in my .Pro file I have the "QT += sql" but this is the minimum requirement to access the QSqlDatabase header
- my postgres Bin is present in the "Enviorment variable paths"
basic project only to test connection db
QT += core gui QT += sql // added the line to access sql greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
the code to connect to the database:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("localhost"); db.setDatabaseName("comments_db"); db.setUserName("postgres"); db.setPassword("dummy_password"); // dummy password db.setPort(5432); bool success = db.open(); if ( !success ) // evaluate to True { qDebug() << "error with the database connection"; // print to the console: qt.sql.qsqldatabase: QSqlDatabase: QPSQL driver not loaded qt.sql.qsqldatabase: QSqlDatabase: available drivers: QSQLITE QMIMER QODBC QPSQL } } MainWindow::~MainWindow() { delete ui; }
the error printed to the console
qt.sql.qsqldatabase: QSqlDatabase: QPSQL driver not loaded qt.sql.qsqldatabase: QSqlDatabase: available drivers: QSQLITE QMIMER QODBC QPSQL error with the database connection
-
for everyone else who encounterd this problem, try to reboot the system first, and if it doesn't work, try to install the latest versions of postgres and the Qt kit. but i'm not sure this will work for you and I can test what the problem was now
-
hi everyone, I already know that are releated topics about this problem but I dont understand how troubleshoot the problem bised the suggestion to copy manually the ".dll" to the executable file location.
Premise
I'm using Sql to connect to a postgres DB for a month now and untill today, I had never got problems.
What I Tryed
- Update Qt6.7.2 to the latest version of Qt (6.8.x)
- Uninstall Postgres 17.1 and install Postgres latest version (17.2)
- In VisualStudioCode I tryed to connect to the database with a query and retrive one of the table's data and works fine.
- in my .Pro file I have the "QT += sql" but this is the minimum requirement to access the QSqlDatabase header
- my postgres Bin is present in the "Enviorment variable paths"
basic project only to test connection db
QT += core gui QT += sql // added the line to access sql greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
the code to connect to the database:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("localhost"); db.setDatabaseName("comments_db"); db.setUserName("postgres"); db.setPassword("dummy_password"); // dummy password db.setPort(5432); bool success = db.open(); if ( !success ) // evaluate to True { qDebug() << "error with the database connection"; // print to the console: qt.sql.qsqldatabase: QSqlDatabase: QPSQL driver not loaded qt.sql.qsqldatabase: QSqlDatabase: available drivers: QSQLITE QMIMER QODBC QPSQL } } MainWindow::~MainWindow() { delete ui; }
the error printed to the console
qt.sql.qsqldatabase: QSqlDatabase: QPSQL driver not loaded qt.sql.qsqldatabase: QSqlDatabase: available drivers: QSQLITE QMIMER QODBC QPSQL error with the database connection
@Andrea_Venturelli
From the output you do haveQPSQL
driver installed and found but there has been an error trying to load it. Often a missing dependency shared library. Set environment variableQT_DEBUG_PLUGINS=1
and re-run with that set. You will get a stream of diagnostic messages. Probably the last one will indicate what the problem loadingPSQL
is. -
@Andrea_Venturelli
From the output you do haveQPSQL
driver installed and found but there has been an error trying to load it. Often a missing dependency shared library. Set environment variableQT_DEBUG_PLUGINS=1
and re-run with that set. You will get a stream of diagnostic messages. Probably the last one will indicate what the problem loadingPSQL
is.@JonB said in Qt6.8 error with Posgres DB connection: "QSqlDatabase: QPSQL driver not loaded":
QT_DEBUG_PLUGINS=1
okay, can you tell me in where I need to paste that line ?
I tryed in the cmd with "Set QT_DEBUG_PLUGINS=1" and run the Qt application and nothing happened, neither in the console built in Qt creator. but I assume I'm doing it wrong -
@JonB said in Qt6.8 error with Posgres DB connection: "QSqlDatabase: QPSQL driver not loaded":
QT_DEBUG_PLUGINS=1
okay, can you tell me in where I need to paste that line ?
I tryed in the cmd with "Set QT_DEBUG_PLUGINS=1" and run the Qt application and nothing happened, neither in the console built in Qt creator. but I assume I'm doing it wrong@Andrea_Venturelli
I know nothing about MS or Visual Studio stuff.
In a Command Prompt indeed type inset QT_DEBUG_PLUGINS=1
(followed by Enter) and then directly run your executable from there.
Feel free to search this forum or Google, you will see that this works from Windows. I cannot comment on your situation or what you might be doing. -
in the meanwhile I reboot the PC and run the program again and now everything work well without giving error... bah I lost 2 hours trying everything and a simple reboot solved it...
-
for everyone else who encounterd this problem, try to reboot the system first, and if it doesn't work, try to install the latest versions of postgres and the Qt kit. but i'm not sure this will work for you and I can test what the problem was now
-
-
for everyone else who encounterd this problem, try to reboot the system first, and if it doesn't work, try to install the latest versions of postgres and the Qt kit. but i'm not sure this will work for you and I can test what the problem was now
@Andrea_Venturelli Your reboot only added the path to the postgresql client libs to your global PATH env var.
See https://doc.qt.io/qt-6/sql-driver.html#how-to-build-the-qpsql-plugin-on-windows
"When you distribute your application, remember to include libpq.dll in your installation package. It must be placed in the same folder as the application executable."