Building With MinGW

From PostgreSQL wiki

Jump to: navigation, search

Most of the people who do development work of the PostgreSQL system do so in a Unix-like environment, so that is where most of the testing is also done. Committed patches are automatically tested on MicroSoft Windows by the Build Farm, but the turn around time on that testing is too slow to be convenient for people who do not work in a Window environment but are touching parts of the code which have cross-platform implications, or for issues which are not explored by the standard build and regression tests but require custom testing. Also, during commitfests patches that relate to Windows often suffer from a shortage of peer reviewers: hopefully instructions in use of MinGW for building on Windows can help that.

Production releases of PostgreSQL for Windows are generally built using MicroSoft's commercial compilers, but these are often not cost-free and can be very hard to use for people more accustomed to a Linux environment. These instructions are intended to help such developers test their code on Windows without much cost and without having to turn themselves into Windows developers.


Set up

If you do not have access to a Microsoft Windows environment, you can rent one from Amazon Web Services. You will need to have some kind of graphical environment, like X-Windows, to enable you to connect to Windows over RDP. Most modern Linux systems will have readily available such a graphical environment and the "rdesktop" program to enable you to connect to Windows.

A t1.micro spot instance has a current price of $0.006 / hour (2013/02/23) and may be free if you have been an AWS customer for less than an year. But it will be slow! Also consider a t2.micro which may be faster if it's available. For a little more, m3.medium is a good choice. As of 2015/09/28 the spot price for this is just under $0.06 per hour.

If you have a Windows system of your own and are willing to install MinGW on it, then the steps of creating and connecting to an Amazon instance can be skipped. If you do run this locally and are not logged on as the administrator, then you can also skip the steps where you create an unprivileged user and run as that user.

* Create an Amazon instance of Windows_Server-2008-SP2-English-64Bit-Base-2012.12.12 (ami-554ac83c), 
  or Windows_Server-2012-R2_RTM-English-64Bit-Base-2015.09.09 (ami-c9cea0ac).
    * make sure you have enabled the RDP port (3389) for the security group in which you launch the instance.
* get the credentials and log in using
    o rdesktop -g 80%  -u Administrator -p 'password' amazon-hostname
* turn off annoying IE security enhancements, and fire up IE
* go to and download latest mingw-get-setup.exe
* run this - make sure to select the Msys and the developer toolkit in addition to the Mingw base.
* navigate in explorer or a command window to C:\Mingw\msys\1.0 and run msys.bat
* run "df" to make sure that the windows Mingw directory is mounted on the virtual /mingw directory. If it's not, edit /etc/fstab with vim and add this line:
    c:/mingw /mingw
* run this command to install some useful packages:
    o mingw-get install msys-wget msys-rxvt msys-unzip
* close that window
* open a normal command window and run the following to create an unprivileged user and open an msys window as that user:
    o net user pgrunner SomePw1234 /add
    o runas /user:pgrunner "cmd /c \mingw\msys\1.0\msys.bat --rxvt"
* if you want to do 64-bit builds, you will need the compiler from the mingw-w64 project (a separate project from the mingw project). Go to 
  and download and run the latest installer (mingw-w64-install.exe). Run it and choose the following options:
    o architecture: x86_64
    o threads: win32
    o location: something like "C:\mingw-w64\x86_64-5.2.0-win32-seh-rt_v4-rev0"
  Then run msys.bat again as the Administrator and edit /etc/fstab and add a line like this:
     c:/mingw-w64/x86_64-5.2.0-win32-seh-rt_v4-rev0/mingw64 /mingw64

The above steps can take a while and download several hundred MB of data, so if you are using an Amazon instance it may be worthwhile to arrange to store this set up for future work so that it does not need to be repeated. (Could someone add instructions on how to do that?)

Build from a tarball

 * Again open rxvt window (if not already open) runas /user:pgrunner "cmd /c \mingw\msys\1.0\msys.bat --rxvt"
    o wget
    o tar -z -xf postgresql-snapshot.tar.gz
    o cd ~/postgresql-9.4devel
 * For a 64-bit build then do:
    o export PATH=/mingw64/bin:$PATH
    o ./configure --host=x86_64-w64-mingw32 --without-zlib && make  && make check
 * For a 32-bit build do instead:
    o ./configure --without-zlib && make  && make check

Make some coffee and do the crossword or read War and Peace - this can take a while.

Installing Git

If you want to build from the git repo instead of a tarball snapshot, which you will need to do if you're doing development, you need to install a git client.

Open and grab the latest version. As of the time of writing this is called Git-2.5.3-32-bit.exe or Git-2.5.3-64-bit.exe. Run this installer. Choose an install path that's easy to manage rather than the default, such as "c:\prog\git". You might get permissions errors. If so, try running again, or else try running as the Administrator. Uncheck all the options unless you think you will need them - you won't need them for command line use from Msys. Don't set up a Start Menu folder, unless you want one - Msys won't need that either. Select "Run git from Windows command prompt" and "Checkout as-is, commit as-is."

After this git should be in your path on Msys, and just work. Verify by running "git --help" in an Msys window started after you installed git.

Build from a git repo

We'll use github's mirror here to check out postgres, to avoid overloading the master repo.

 * Again open rxvt window (if not already open) runas /user:pgrunner "cmd /c \mingw\msys\1.0\msys.bat --rxvt"
    o git clone
    o cd postgres

Then follow the same 64-bit or 32-bit build instructions as for building from a tarball.


After you have built you can install by running

 make install

Following this you need to copy the libpq dll from the installation lib directory to the installation bin directory. This lets pg_ctl and psql and other client programs work.

Using psql interactively

The psql client doesn't work well in the rxvt terminal emulator, and appears to hang. Instead you can open a non-rxvt shell by omitting the "--rxvt" flag when opening a session, and psql works as expected. It also works in the normal Windows command window, and in the Windows Power Shell window. None of these builds have readline installed, so you don't get psql history, command completion and so on. If you want to do lots of work with psql on Windows, the best way might be to build and run psql under Cygwin, where readline is fully supported. That's what I do.


Cross Compiling

Another alternative to needing/using a windows box is to cross compile postgres from a linux box, ex (ubuntu):

 # skip all the above steps, just do this:
 $ sudo apt-get install mingw-w64
 # download the source, cd into it, same instructions as above
 $ ./configure --host=i686-w64-mingw32 --without-zlib --prefix=... # 32 bit
 $ ./configure --host=x86_64-w64-mingw32 --without-zlib --prefix=... # 64 bit

Then you can run to test it using wine [or copying it to a windows box and running natively, of course].

 $ sudo apt-get install wine
 $ wine /full/path/to/psql.exe # etc. You can follow First_steps after its installed (actually, for any of the build mechanisms).


You can install cygwin and build it "just like you would in linux" using cygwin's packages (gcc etc.), from inside a windows box.


You can also run windows inside a virtualbox VM inside your Linux box.

Personal tools