Category Archives: Databases

PostgreSQL driver for Windows and Linux

Due to license incompatibilities with the GPL, the QPSQL plugin needed to connect to a PostgreSQL database is not provided with open source versions of Qt.

This tutorial is about setting up a compete Qt development environment with PostgreSQL support for Windows 7 and Ubuntu 11.10 (Oneiric Ocelot). It also describes all steps necessary to build the QPSQL plugin for Windows using Qt SDK with MinGW.

Windows 7

1. Download Qt SDK for Windows at and install it.

The current version is Qt_SDK_Win_offline_v1_1_4_en.exe and its default installation folder is C:\QtSDK.

2. Download the source code associated to your Qt SDK version at

Qt SDK version 1.1.4 uses Unzip its content into C:\Qt\4.7.4 folder for instance.

3. For Windows 7 x64 (64-bit) you need to download a 32-bit version since it is not possible, at the moment, to compile the 64-bit version of PostgreSQL plugin.

Download PostgreSQL at and install it.

The current version is postgresql-9.1.1-1-windows.exe. The default PostgreSQL installation folder is C:\Program Files (x86)\PostgreSQL\9.1 (short path is C:\Progra~2\PostgreSQL\9.1) for Windows 7 x64 (64-bit) and C:\Program Files\PostgreSQL\9.1 (short path is C:\Progra~1\PostgreSQL\9.1) for Windows 7 x86 (32-bit).

4. Open Start > All Programs > Qt SDK > Desktop > Qt 4.7.4 for Desktop (MinGW).

5. Run these commands to build the QPSQL plugin (change according to your environment and make sure you use short path type):

set QTSDK_SQLDRIVERS_PATH=C:\QtSDK\Desktop\Qt\4.7.4\mingw\plugins\sqldrivers
set QT_SRC_PATH=C:\Qt\4.7.4
set POSTGRES32=C:\Progra~2\PostgreSQL\9.1

cd %QT_SRC_PATH%\src\plugins\sqldrivers\psql

qmake "INCLUDEPATH+=%POSTGRES32%\include" "LIBS+=%POSTGRES32%\lib\libpq.lib"

mingw32-make debug
mingw32-make release

copy release\libqsqlpsql4.a %QTSDK_SQLDRIVERS_PATH%
copy release\qsqlpsql4.dll %QTSDK_SQLDRIVERS_PATH%
copy debug\libqsqlpsqld4.a %QTSDK_SQLDRIVERS_PATH%
copy debug\qsqlpsqld4.dll %QTSDK_SQLDRIVERS_PATH%

These commands build a Release (qsqlpsql4.dll, libqsqlpsql4.a) and a Debug (qsqlpsqld4.dll, libqsqlpsqld4.a) versions of the QPSQL plugin. These 4 files are then copied into Qt SDK’s SQL drivers folder.

7. The QPSQL plugin uses additional DLLs located in bin folder under the PostgreSQL installation folder. You have to copy these DDLs to the same directory as your application binary.


Ubuntu 11.10 (Oneiric Ocelot)

1. Install Qt Creator.

$ sudo apt-get install qtcreator

2. Install PostgreSQL.

$ sudo apt-get install postgresql

3. Optionally, you can install pgAdmin III, a handy GUI for PostgreSQL administration.

$ sudo apt-get install pgadmin3

4. Install PostgreSQL plugin for Qt 4.

$ sudo apt-get install libqt4-sql-psql


Follow the following steps to check that everything is functional.

1. Create an empty Qt console application project.

2. Make sure you have QtSql Module in your project configuration file (.pro file) by adding this line:

QT += sql

3. Update the source code as follows:

#include <QtSql>

int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    qDebug() << "Has a valid QPSQL driver: " << (db.isValid() ? "YES" : "NO");

    return a.exec();

4. Compile and run the application (Under Windows, do not forget to copy the required DLLs from the bin folder located in the PostgreSQL installation folder). You should see:

Has a valid QPSQL driver: YES