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.

Contents

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 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 runas that user.

* Create an Amazon instance of Windows_Server-2008-SP2-English-64Bit-Base-2012.12.12 (ami-554ac83c) 
    * 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 http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/ and download latest mingw-get-inst
* run this - make sure to select the Msys and the developer toolkit in addition to the C compiler
* navigate in explorer or a command window to C:\Mingw\msys\1.0 and run msys.bat
* 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, in the rxvt window install the extra compiler:
    o wget "http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Automated%20Builds/mingw-w64-bin_i686-mingw_20111220.zip/download"
    o mkdir /mingw64
    o cd /mingw64
    o unzip ~/mingw-w64-bin_i686-mingw_20111220.zip
    o cd

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 http://ftp.postgresql.org/pub/snapshot/dev/postgresql-snapshot.tar.gz
    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 http://code.google.com/p/msysgit/downloads/list and grab the latest version. As of the time of writing this is called Git-1.8.3-preview20130601.exe. Run this installer. Choose an install path that's easy to manage rather than the default, such as "c:\prog\git". 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".

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 https://github.com/postgres/postgres.git
    o cd postgres

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

Installing

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.

Alternatives

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).

Cygwin

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

Virtualbox

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

Personal tools