Compilation of PostgreSQL 8.3.7 on Visual Studio 2008

  • Posted on: 17 May 2009
  • By: Michał Turecki

Today I have encountered few problems while trying to compile PostgreSQL on Windows. PostgreSQL designed to work in *nix environments and it's native build system works flawlessly on Linux just as expected. Unfortunatelly on Windows this build system is used by minority of applications, partially using Microsoft's own version of make: nmake.

In PostgreSQL only libpq.dll can be compiled using nmake in Visual Studio Command Prompt which sets all necessary environment variables for compiler and linker (INCLUDE, LIBPATH etc, type set in command prompt to verify these paths).

Make sure you have installed most recent version of the Windows SDK before compiling postgresql, current version is 6.1 and it's default installation path is C:\Program Files\Microsoft SDKs\Windows\v6.1. To compile only libpq.dll in command prompt type following:

nmake /f win32.mak
or optionally:
nmake /f win32.mak DEBUG=1

To build whole project you need to satisfy all requirements regarding win32 build, specified in PostgresSQL documentation ( ). I used ActiveState Perl, but as mentioned in documentation standard distribution would be sufficient, just be sure to update src\tools\msvc\ if perl version differs from 5.8 ( replace \lib\CORE\perl58.lib with valid perl library path ). When installing these requirements you can specify same folder for all installations, even ActiveState Perl, for example C:\pgbuild, then only bin directory in C:\pgbuild folder must be added to PATH environment variable so all tools could be found by PostgreSQL build scripts.

PostgreSQL 8.3.7 default win32 build scripts are prepared for Visual Studio 8 (2005) and Windows Platform SDK obsoleted by Windows SDK so few changes are necessary to build PostgreSQL:

  1. In replace ProjectType="Visual C++" Version="8.00" with ProjectType="Visual C++" Version="9.00"
  2. In src\include\port\win32.h replace #define _WIN32_WINNT 0x0500 with #define _WIN32_WINNT 0x0501 to satisfy requirement for IPPROTO_IPV6 conditionally defined in Windows SDK. Updating src\include\pg_config_os.h might be also necessary.
  3. To build libpq.dll I have needed to supply src\interfaces\libpq\libpq.rc file by copying it from src\interfaces\libpq\, elegant solution would be to add:
    "libpq.rc" :
    if not exist "libpq.rc" copy "" "libpq.rc"

    to src\interfaces\libpq\win32.mak just before line beginning with: CPP_PROJ=.
  4. Update paths in src\tools\msvc\ to match previously installed dependencies.

To build PostgreSQL just type build in Visual Studio Command Prompt in src\tools\msvc folder. All possible problems would appear yellow and red and generally would be simple to solve by satisfying dependencies like include files (eg. zlib.h) or required libraries (zlib.lib and zdll.lib which need to be grabbed from the net).

Take a look at other tools in src\tools\msvc, clean would be your inseparable friend and install with destination folder passed as parameter will ease an installation of your fresh PostgreSQL build.

Have fun hacking pg and feel free to comment your experiences.