Building PostgreSQL client library using MinGW under WinXP SP3

Preface

Deploying Windows applications which work with PostgreSQL you often need to provide some client libraries, such as libpq.dll, with it. This particular library can be easily found in the PostgreSQL installation folder. As we all know modern versions of PostgreSQL are built with Microsoft Visual Studio 2005, which in turn makes us depend on MSVC run-time library. For example, libpq.dll library which shipped with PostgreSQL 8.3.5 version depends on:

  • comerr32.dll
  • gssapi32.dll
  • k5sprt32.dll
  • krb5_32.dll
  • libeay32.dll
  • libiconv2.dll
  • libintl3.dll
  • msvcr80.dll
  • ssleay32.dll

Here we can face very annoying things. What if user hasn’t the latest MSVC RTL? Of course there is a workaround – he can install Microsoft Visual C++ 2005 Redistributable Package (vcredist_x86.exe). But what if your tool is only 300Kb? Why make it 2.6Mb heavier?

In this article we’ll try to get rid of all these dependencies by building client libraries with MinGW.

Installing MinGW

Go to Mingw.org. If you first time here I suggest you to spend some time studying this site. At least you should find out differences between MinGW and MSYS.

Then at the Downloads Section get Automated MinGW Installer. I used MinGW 5.1.4 as it was the latest release at the moment.

Here are the screenshots of the installation process where you can see what I had chosen:

Download and Install option

Current package

Minimal type

I have MinGW installed into E:\MinGW\ and hereafter I will use this path.

Installing MSYS

Now it’s time for MSYS. Here you can find the detailed installation instructions. In my environment MSYS was installed into E:\MSYS\1.0\ path.

Downloading PostgreSQL sources

Version 8.3.5 of PostgreSQL was the latest official release at the moment of writing this article. The latest version could be found using File Browser. After downloading the archive, I have extracted its contents into E:\postgresql-8.3.5\ folder.

Let’s configure it

Run MSYS (a cyan “M” on your Desktop). Enter command to change the current directory to E:\postgresql-8.3.5:

$ cd /e/postgresql-8.3.5/

The next step is to configure the source tree for our system using configure script.

Run:

$ ./configure

If everything worked fine then the last lines of the output should look like this:

configure: error: zlib library not found
If you have zlib already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-zlib to disable zlib support.

ZLib library is used for pg_dump and pg_restore utilities. Since I don’t need them right now (we need only libpq.dll) we should run modified command:

$ ./configure --without-zlib

We’ve configured the sources and they are ready to be built with make command. If you do not need SSL support then you can skip the next section and go streight to “Let’s build it” one.

Let’s configure it with SSL support

If you need SSL support (and you probably do) you should let configure know about it. To do this we need pass additional parameter --with-openssl:

$ ./configure --without-zlib --with-openssl

But we’ve got an error:

...
checking for CRYPTO_new_ex_data in -leay32... no
configure: error: library 'eay32' is required for OpenSSL

We need to install OpenSSL. The latest version can be found at http://www.openssl.org/ (0.9.8i at the moment). Download Windows pre-compiled binaries. Choose Win32 OpenSSL v0.9.8i (not Light version).

Install OpenSSL wherever you want (E:\OpenSSL in my case). Then go to lib\MinGW subfolder (E:\OpenSSL\lib\MinGW in my case) and copy all files into MinGW lib folder (E:\MinGW\lib in my case).

Let’s try again:

$ ./configure --without-zlib --with-openssl

Oh, it’s better now. configure found eay32 library. But now we have problem with ssleay32 library.

...
checking for CRYPTO_new_ex_data in -leay32... yes
checking for SSL_library_init in -lssleay32... no
configure: error: library 'ssleay32' is required for OpenSSL

The thing is PostgreSQL configure expects ‘ssleay32′ library will be called libssleay32.a, but we just have ssleay32.a. Well, let’s make configure happy – just rename ssleay32.a to libssleay32.a.

Finaly run:

$ ./configure --without-zlib --with-openssl

Let’s build it

If you want to build the whole server, then you probably need something like this:

$ cd /e/postgresql-8.3.5/src/
$ make all

Since we need only libpq.dll (at least I need only this for PostgresDAC) we should execute:

$ cd /e/postgresql-8.3.5/src/interfaces/libpq/
$ make

Oops, we got an error:

...
In file included from e:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/security.h:38,
from libpq-int.h:57,
from fe-auth.h:18,
from fe-auth.c:48:
e:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sspi.h:60: error: syntax error before "SECURITY_STRING"
In file included from e:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/security.h:39,
from libpq-int.h:57,
from fe-auth.h:18,
from fe-auth.c:48:
e:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/ntsecpkg.h:123: error: syntax error before "SECURITY_STRING"
...

This issue was discussed in the Hackers PostgreSQL mailing list and discussion could be found in the archive.

The solution is to add an additional header file to
postgresql-8.3.5/src/include/libpq/libpq-be.h
and
postgresql-8.3.5/src/interfaces/libpq/libpq-int.h:


...
#ifdef ENABLE_SSPI
#define SECURITY_WIN32
#include <ntsecapi.h> <——- Add this include
#include <security.h>
#undef SECURITY_WIN32
...

Just interesting, will this patch be present in 8.4.x branch of PostgreSQL sources?

Now execute:

$ cd /e/postgresql-8.3.5/src/interfaces/libpq/
$ make

That’s all.

Gimme the binaries

Those who have no time neither desire playing with MinGW can get “Deployment libraries built with MinGW environment” from PostgresDAC Download page.

P.S.

Special thanks to Anthony Caduto and Michael Smirnov for help.

About these ads

Nerd.

Tagged with: , , , , , , ,
Posted in Coding
10 comments on “Building PostgreSQL client library using MinGW under WinXP SP3
  1. pashagolub says:

    By the way. Those who have no time neither desire playing with Microsoft Visual Studio 2005 or download full PostgreSQL 8.3.5 installation, can get “Deployment libraries built with MS Visual C++ environment” from PostgresDAC Download page either.

  2. Md. Abdur Rahman says:

    Dear Sir,
    With due respect, I like to mention that your documentation really really inspired me. I was trying to build the postgresql in windows. I lost my hope and finally your tutorial gave me some hope. I have followed your steps and could build it. I have some development target. May I request you as an expert to help me a bit. I am eagerly waiting to hear from you.

  3. Md. Abdur Rahman says:

    One more question, does the above steps also create a server build? Once I am following these steps, the server executable are not created. Can you help me a bit in creating a server build? I appreciate your comments

  4. Deepti says:

    Hi I know this is quite an old blog but I am stuck up with the same error “Driver not loaded” and went through your blog..I found the instructions very easy to follow and understand…
    I followed them but it seems like my problem is still not solved…I configured and compiled PostgreSQL-8.4 on windows xp sp3,with msys..
    It didn’t show me any errors but the only folders created are bin,lib and data out of which bin is empty and lib has following files liblibpq.a, libpq.a, LIBPQ.def

    Could u please help me solving this issue…
    I am really desperate and appreciate ur help…

    Regards
    Deepti

  5. Mamnun says:

    Many many thanks. Just followed the steps on a Win7 x86 machine. Worked flawlessly(without zlib and openssl).

  6. [...] Building PostgreSQL client library using MinGW under WinXP SP3 December 20088 comments 3 [...]

  7. Vivian says:

    2 days of hammering my head against the brick wall trying to get my QT app to talk to postgres. Worked great under Linux, sucked unuder windows. Thanks so much for pointing me in the right direction about using MinGW built pglib instead of the MVS one I had been trying to get the QT wrapper to work with.
    My installer now works great now on a brand new Windows machine that has nothing else loaded.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Map of wandering
Follow

Get every new post delivered to your Inbox.

Join 73 other followers

%d bloggers like this: