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. Qt6.8 error with Posgres DB connection: "QSqlDatabase: QPSQL driver not loaded"
Forum Updated to NodeBB v4.3 + New Features

Qt6.8 error with Posgres DB connection: "QSqlDatabase: QPSQL driver not loaded"

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 559 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.
  • A Offline
    A Offline
    Andrea_Venturelli
    wrote on last edited by
    #1

    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
    
    JonBJ 1 Reply Last reply
    0
    • A Offline
      A Offline
      Andrea_Venturelli
      wrote on last edited by
      #6

      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

      Christian EhrlicherC 1 Reply Last reply
      0
      • A Andrea_Venturelli

        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
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @Andrea_Venturelli
        From the output you do have QPSQL driver installed and found but there has been an error trying to load it. Often a missing dependency shared library. Set environment variable QT_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 loading PSQL is.

        A 1 Reply Last reply
        1
        • JonBJ JonB

          @Andrea_Venturelli
          From the output you do have QPSQL driver installed and found but there has been an error trying to load it. Often a missing dependency shared library. Set environment variable QT_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 loading PSQL is.

          A Offline
          A Offline
          Andrea_Venturelli
          wrote on last edited by
          #3

          @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

          JonBJ 1 Reply Last reply
          0
          • A Andrea_Venturelli

            @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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Andrea_Venturelli
            I know nothing about MS or Visual Studio stuff.
            In a Command Prompt indeed type in set 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.

            1 Reply Last reply
            1
            • A Offline
              A Offline
              Andrea_Venturelli
              wrote on last edited by
              #5

              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...

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Andrea_Venturelli
                wrote on last edited by
                #6

                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

                Christian EhrlicherC 1 Reply Last reply
                0
                • A Andrea_Venturelli has marked this topic as solved on
                • A Andrea_Venturelli

                  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

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @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."

                  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

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved