Building the QPSQL plugin on Windows using MinGW

From QtCentreWiki

Jump to:navigation, search

How to Build the Plug-in

At the time of writing I am using Qt 4.5.3 OpenSource SDK.

Pre-requesties: [For Windows users] You need make tools if you are not using microsft compiler. For MS compilers use nmake. If make tool is not available, download make tools from http://www.steve.org.uk/Software/make/. Extract it to some folder and update environmental variable PATH for the make directory. (I extracted make.zip to system32) You also need to download mingw-utils from sourceforge website.(reimp tool, not availale with default installation of Qt4.5.3)


  1. Open a Qt Command Prompt and go to wherever you installed the PostgreSQL server (C:\PostgreSQL\8.3 as example).
  2. Goto the sub-directory lib and run reimp libpq.lib to produce the liblibpq.a and libpq.def file.
  3. In file libpq.def remove "_" from definitions.
  4. dlltool --input-def libpq.def --output-lib libpq.a --dllname libpq.dll

This is the import library to use with MinGW.

  1. Go to %QTDIR%/src/plugins/sqldrivers/psql.
  2. Run the following command: qmake -o Makefile "INCLUDEPATH+=C:\PostgreSQL\8.3\include" "LIBS+=C:\PostgreSQL\8.3\lib\libpq.a" psql.pro
  3. Run make - this should build the qsqlpsql.dll and libqsqlpsql.a files in the %QTDIR%/plugins/sqldrivers directory.

Now the plug-in should load properly. You may have to empty the plug-in cache before everything works though.

[For Windows users] If you get an error : g++: debug\qsqlpsqld_resource_res.o: No such file or directory

  1. Go to %QTDIR%/src/plugins/sqldrivers/psql and copy debugqsqlpsqld_resource_res.o file to debug directory

and rename it to qsqlpsqld_resource_res.o


After successful completion of the above steps and Qt application says QSqlDatabase: QPSQL driver not loaded.

You may follow these steps.

  1. Get a copy of dependency walker for your windows version from here - http://www.dependencywalker.com/.
  1. Load the .dll file from %QTDIR%/plugins/sqldrivers/psql in the Dependecy Walker.
  If no dependecy issues are reported, your application will work fine.
  1. If any missing dependencies are reported, find the missing .dll path on windows and update your environmental variable PATH. After updating PATH variable recheck your dependency using dependency walker.

After these you can check if driver is loaded using the code:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qDebug() << QSqlDatabase::drivers();
    return a.exec();
}

Code output: ("QSQLITE", "QODBC3", "QODBC","QPSQL7","QPSQL")

How to Build the Plug-in in Windows 7 Pro x64, PostgreSQL 9 and QtSDK 1.1

If you are about to commit suicide, these steps will you guide through building your own

QPSQL plugin.

System configuration

  1. Windows 7 Pro x64
  2. PostgreSQL 9.0.4
  3. QtSDK 1.1 (the latest one)

Detailed commands

Step 1

Download a 32BIT VERSION of PostgreSQL! It is available at http://www.postgresql.org.

Qt in SDK is 32 bit pre-builded, so if you are not going to compile 64 bit Qt, download the 32 bit PostgreSQL installation.

Step 2

Install PostgreSQL to destination C:\psql.

Step 3

Download QtSDK 1.1 online installer (the latest one with new folder sturcture) available

at http://qt.nokia.com.

Step 4

Run QtSDK installer and choose a custom setup. Install to C:\QtSDK.

You are going to build a part of Qt, so you need a source codes. Check an option

Miscellaneous > Qt Source > 4.7.3 (or latest if available).

Finish the installation.

Step 5

I've prepared a package with all necessary executables needed for building. So download

it at www.cox.cz/qt/make.zip.

If you are going to use your own, DO NOT USEmingw32-make.

Extract it to C:\.

Step 6

Add paths of PostgreSQL bin, include and lib folders to Windows PATH variable. Mind the

semicolon separator.

 C:\psql\bin;C:\psql\include;C:\psql\lib

Step 7

Run Qt Console at Start > Qt SDK > Desktop > Qt 4.7.3 for Desktop (MinGW)

. If your Qt version differs, the filename may be a little bit different.

Step 8

Run these commands:

 cd C:\QtSDK\QtSources\4.7.3\src\plugins\sqldrivers\psql
 qmake "INCLUDEPATH+=C:/psql/include" "LIBS+=C:/psql/lib/libpq.lib" psql.pro
 c:\make\make

You should now see no errors in text output, only one at the end.

 g++: debug\qsqlpsqld_resource_res.o: No such file or directory

Check folder C:\QtSDK\QtSources\4.7.3\src\plugins\sqldrivers\psql if there is a file debugqsqlpsqld_resource_res.o. If yes, let's continue.

 move debugqsqlpsqld_resource_res.o debug\qsqlpsqld_resource_res.o

And once again make

 c:\make\make

Step 9 (Optional)

If you need also a release lib of QPSQL driver.

 c:\make\make release
 move releaseqsqlpsql_resource_res.o release\qsqlpsql_resource_res.o
 c:\make\make release

Step 10

Copy the builded files from folder:

 copy debug\libqsqlpsqld4.a C:\QtSDK\Desktop\Qt\4.7.3\mingw\plugins\sqldrivers
 copy debug\qsqlpsqld4.dll C:\QtSDK\Desktop\Qt\4.7.3\mingw\plugins\sqldrivers

If you were creating also release libs:

 copy release\libqsqlpsql4.a C:\QtSDK\Desktop\Qt\4.7.3\mingw\plugins\sqldrivers
 copy release\qsqlpsql4.dll C:\QtSDK\Desktop\Qt\4.7.3\mingw\plugins\sqldrivers

Step 11

Check that everything is functional using the example bellow. Create empty Qt console aplication.

// .pro file
 
QT += sql
 
// main.cpp
 
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QSqlDatabase>
#include <QStringList>
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
 
    qDebug() << "Available drivers: " << QSqlDatabase::drivers();
 
    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
 
    qDebug() << "addDatabase() " << (db.isValid() ? "VALID" : "ERROR");
 
    return a.exec();
}

You should see QPSQL driver there and database addition should be VALID.