QT PostgreSQL connection
up front I would like to say that I am new to QT 3.1.1 OpenSource and C++. For 2 days I have been searching for a solution and would appreciate any help. I would like to establish a PostgreSQL connection with my application on Win8/64bit. My first program is running, but I keep getting an error trying to connect to PostgreSQL. I would like to establish a connection and later on run SQL queries with my code.
@QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
My code looks like this
@// Within .pro
QT += sql
// Within .cpp
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
if(!db.open()) ui->label->setText("Connection Failed!"); else ui->label->setText("Connected");
I know QT is probably missing a driver or a path to find driver for PostgreSQL due to license issues. What files are missing and where do they belong? Where do I get those files? Or am I missing some code? I tried to find a solution with QT Wiki/Doc, but for a beginner it's hard.
This files must be in the folder of your application or in PATH:
They are in <PostgreSQL folder>\bin
Also in subfolder "sqldrivers" you have to add this file: "qsqlpsql.dll".
You can find it in "<QTDIR>\plugins\sqldrivers".
Make sure that both version of Qt and PostgreSQL are same type (64-bit or 32-bit). I build an application with Qt 5 (Mingw-32) and it didn't work with 64-bit version of PostgreSQL. This can be my fault, but still consider this case.
clochydd last edited by
Hi, and welcome to DevNet,
to start using PostgreSQL with Qt you have to build the QPSQL driver, see "here (QPSQL for PostgreSQL): ":http://qt-project.org/doc/qt-5/sql-driver.html#qpsql
thank you for providing a very easy to follow instruction. I copy paste the following files from PostgreSQL to the folder where my QT application (I am currently working on) is located:
I am running Win8/64bit and I also installed 64bit QT version. However, PostgreSQL is a 32bit version I believe. It works with a 32bit external program. The mentioned "qsqlpsql.dll" is already present within "<QTDIR>\plugins\sqldrivers"
I am still getting the same error. Do I have to install a 32bit QT version or is there a way to work around this issue?
My goal for the application is to use as much RAM as possbile, therefore 64bit.
As far as I know, Qt Mingw (which I use) has only 32-bit version pre-build. If you have 64-bit version and didn't build it yourself, then you probably have version VS 2013. I didn't use it, but I think you can compile with it your code to 32-bit application and see if it work.
You can also use Dependency Walker on qsqlpsql.dll to check what DLLs are needed. For VS 2013 they may be different.
I just find video instructions how to use plugin of PostgreSQL and how to make it yourself:
"Qt 5.3 MinGW + PostgreSQL 9. Make SQL driver and database connectiion on Windows 7":
there is no video for me on this site
New setup...Now I have Visual Studio 2013 installed. I downloaded, installed and ran the following Qt files on Windows 8.1 (64bit):
a) Qt 5.3.1 for Windows 32-bit (VS 2013, 559 MB)
b) Visual Studio Add-in 1.2.3 for Qt5
Everything went fine and I configured the correct qmake.exe path with Visual Studio 2013->Qt5->Options which is C:\Qt\Qt5.3.1\5.3\msvc2013. Therefore Qt does run within Visual Studio 2013 now.
I am still trying to connect to postgreSQL 9.0 (32bit) with my Qt application and I am still facing the error:
@QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7@
I now there are files missing, but I have no idea how to solve this. I found an old thread here:
in the executable’s path make sure you have the following files (I suppose you’re using windows):
you may also need:
Where do I get all those files? Where do I have to move them?
I also tried to use Dependency Walker, but found no solution there either since I don't know how to handle it.
Since I am new to C++, Visual Studio and Qt it would be very kind if someone would help with a step by step solution on how to solve this.
Sorry, I fixed the link. Look at it!
Did you try with the list of files which I posted before?
They are the only files related to PostgreSQL and must be placed in the folder where your program is.
You also need other files for deploying your application, but there is another theme on this site about that.