Adding PostgreSQL support



  • Hey,

    I'm currently having issues to get my old QT postgresql build working on a new pc.
    It was once working on my old one but I simply can't remember the steps for it.
    I went through the sqldriver tutorial https://doc.qt.io/qt-5/sql-driver.html, but as far as I remember I didn't build anything my self the last time.
    Currently I've simply installed PostgreSQL on my Windows PC

    I've added follwing to my .pro

    QT       += core gui sql
    
    INCLUDEPATH += E:\Development\PostgreSQL\bin
    LIBS += E:\Development\PostgreSQL\lib
    
    

    The QT seems to complain about the include paths, but VisualStudio seems to ignore that.

    Now the strange thing is, When debugging through the app, it seems like it fails to open the DB connection and can't insert anything to my Database obviously.

    And i get follwing debug output:

    QSqlDatabase: QPSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
    

    But when I start the .exe directly through the windows explorer, everything works fine, which doesn't make any sense for me.

    Can anyone help?


  • Qt Champions 2018

    @Slei said in Adding PostgreSQL support:

    E:\Development\PostgreSQL\bin

    Are you sure this is the directory where include files are located?
    It should be something like E:\Development\PostgreSQL\include



  • yup thats correct, that was just something from my old project.
    tbh, I don't even know if those lines have any affect at all, probably I don't even need those.
    I also haven't seen any include/lib dir requirements in the tutorials.

    also libs has to be added with an -L prefix :
    LIBS += -L"E:\Development\PostgreSQL\lib"


  • Lifetime Qt Champion

    Hi,

    The PostgreSQL dlls are likely not found. Go to the Run part of the Project panel and add the path to them to the PATH environment variable.



  • @SGaist
    I've added the postgresql lib path to the windows PATH env. variables already:

    postgresql install path "E:\Development\PostgreSQL\lib" and "E:\Development\PostgreSQL\bin"

    additionally I've added those lines now to the "Run Environment" Path variable settings in "Run" bit it seems like still cannot load the drivers

    The paths seem to work for executing the exe directly, since I can successfully execute sql stuff on the db, so maybe i'm still not adding the libs correctly



  • On visual studio I've now added the postgresql bin path directly and it seems to work. so there must be an issue with adding the libs inside the .pro file. Shouldn't it be enough to have those paths in the PATH env. variable?



  • Ok, not sure if that was the issue at the end, but I've moved the settings from Accoun Environment Variables to System Variables and now it seems to work, even though the account variables are in the Qt Run Path...

    About the Qt tutorial...
    In there it says I have to build the drivers myself, but in the end, I never had to build anything at all, can anyone explain what this is for? Is it just for building qt from source?
    For me this was quite confusing since it's the main tutorial that pops up when searching for "qt postresql tutorials"


  • Lifetime Qt Champion

    Which tutorial are you refereeing to ?



  • @SGaist https://doc.qt.io/qt-5/sql-driver.html it's not a tutorial but something that comes first when you search for one.
    At the beginning I though this step was necessary to be able to work with my db.


  • Lifetime Qt Champion

    The pre-built binary comes with a set of drivers. What happens usually is that you have to tell your application where to find the corresponding dependencies. If the version of of PostgreSQL you installed is different that the one used to build the plugin, then you will have to re-build it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.