Compiling PostgreSQL, PostGIS and WKTRaster on Windows

06.06.2009 15:53 ·  GIS  ·  postgis, wktraster, howto

To some extent, PostGIS can be seen as a free, open-source alternative to ArcSDE, but until recently it lacked support for raster data. Lately, a WKTRaster project has been started to fill this gap and develop an extension to support raster data in PostGIS. Perhaps in the near future, PostGIS will be able to offer its users functionality comparable to ArcSDE.

There was an idea to compare the speed of loading a large raster in ArcSDE and PostGIS. Since there are no ready-to-use binary builds of WTKRaster, it is necessary to build and configure the PostgreSQL + PostGIS + WKTRaster bundle before performing any tests.

We will build the latest stable version of PostgreSQL (8.3.7 at the time of writing), the latest SVN version of PostGIS (1.4.0SVN at the time of writing), and the latest SVN version of WKTRaster (0.1.6SVN at the time of writing). This post is based on the official build instructions and blog posts by Mateusz Loskot.

Preparation

First, we need to install and configure a development environment based on MinGW. In the following steps, I will assume that MinGW and MSYS will be installed in the default directories (C:\MinGW and C:\MSYS\1.0, respectively) and that the source code of all components is located in subdirectories of the src directory on drive D:\ (e.g. D:\src\libtool-2.2).

Let’s start.

MinGW installation

MinGW (Minimalist GNU for Windows, formerly mingw32) is a port of the GNU Compiler Collection (GCC) compiler for Microsoft Windows that includes a set of free libraries and header files for the Windows API. It allows developers to create Microsoft Windows applications. MinGW includes extensions to the Microsoft Visual C++ runtime library to support C99 (a modern dialect of C).

Download the MinGW Installer (version 5.1.4 at the time of writing this post). Don’t be surprised by the small size of the downloaded file — it’s a network installer, the full installation will take about 170 MB of disk space. Then

  1. run the installer
  2. select “Download and Install” and follow instructions
  3. when you get to the component selection, select all of them except Ada. However, if you don’t care about download size or disc space, select everything.

It is better to leave the default directory (C:\MinGW) unchanged. In the following steps I will assume that MinGW is installed in the default location.

MSYS installation

MSYS (Minimal SYStem) provides the user with a POSIX-compatible environment and a set of console utilities for building UNIX programs. MSYS was originally designed to allow the execution of configuration and build scripts (Makefiles), which are often used to build open source software. Later, many people began to use it as an alternative to the Windows cmd.exe command line interface.

A few steps are required to install MSYS.

MSYS

You can download MSYS Installer from here.

  1. start the installation and follow the wizard instructions
  2. leave the destination directory (C:\msys\1.0) unchanged
  3. after installation, a window will appear asking if you want to perform post-installation customisation — select “Yes”
  4. when asked if you have MinGW installed, select “Yes”
  5. specify the MinGW directory (if you did not change the default path when installing MinGW, write c:/mingw)

MSYS DTK

Next, download and install the stable build of the MSYS Development Toolkit. The installation process should be straightforward, just follow the wizard instructions and accept the default options.

MSYS Core

We also need to download the MSYS Core 1.0.11 package. Unpack the archive to C:\msys\1.0, replacing any existing files.

Additional GnuWin32 packages

The full list of packages can be found at http://gnuwin32.sourceforge.net/packages.html. However, for successful compilation, we only need to download and install:

When installing these packages, specify C:/MinGW as the destination directory.

A few words about libiconv

The authors of the PostGIS build instructions recommend building libiconv from source. However, I have not encountered any problems using the binary package. If you want, you can download the sources of the library (I used version 1.13.1), unpack them into a source folder (e.g. D:\src\libiconv-1.13.1) and build it. All you need is to build libiconv:

  1. open the MSYS console using a shortcut on the Desktop or an item in the Start menu

  2. in the console type the following commands one by one, ending each command with the Enter key

    cd /d/src/libiconv-1.13.1
    ./configure --prefix=/c/mingw && make && make install
    

m4 macro processor

Practice has shown that the macro processor installed with MSYS is quite old and needs to be updated. So, download m4-1.4.7-MSYS.tar.bz2, unpack it in the sources directory, and copy the file m4.exe to C:\msys\1.0\bin.

Environment variables

We are now finished with the MinGW/MSYS installation. But to make life a bit easier and to tell the shell where to look for MinGW and/or GnuWin32 tools, it is necessary to update the environment variables. This step is not mandatory and can be skipped.

To edit environment variables:

  1. right-click on the “My Computer” icon and select “Properties” from the context menu
  2. open the “Advanced” tab, then click on the “Environment Variables” button
  3. in the “System Variables” group find the line saying PATH and double-click on it
  4. a dialog box with two input fields will appear, the bottom field contains the value of the PATH variable. Be careful and do not delete existing entries. Go to the end of the line and add ;C:\MinGW\bin. Don’t forget the semicolon — it’s used to separate entries. When you are done with editing, click “OK”
  5. create the HOME variable. To do this, click on the “New” button, enter HOME in the top field and C:\msys\1.0\home in the bottom field. Press “OK” to save changes
  6. close all windows one by one by clicking “OK”.

autotools

These tools are widely used by developers and include autoconf, automake and libtool. Download the sources from the gnu.org server

Extract each archive to a separate subdirectory within the source directory. Start the MSYS console (using the Desktop icon or from the Start menu) if it is not already running, and build the autotools components one by one.

cd /d/src/autoconf-2.63
configure --prefix=/c/mingw && make && make install
cd /d/src/automake-1.9
configure --prefix=/c/mingw && make && make install
cd /d/src/libtool-2.2
configure --prefix=/c/mingw && make && make install

Compilation

The preparation is over, let’s get down to what this was all about — building PostgreSQL and spatial extensions. First, we will build PostgreSQL, then PostGIS and WKTRaster.

Building PostgeSQL

Download the source code of the latest stable version of PostgreSQL (8.3.7 at the time of writing) from http://www.postgresql.org/ftp/source/]. After unpacking the archive to D:\src\postrgesql, we will find there three directories: backend, postgresql-8.3.7 and rc. Now, we need to move files from the D:\src\postgresql\backend\utils\mb\conversion_procs\euc_jis_2004_and_shift_jis_2004\ directory to the D:\src\postgresql\postgresql-8.3.7\src\backend\utils\mb\conversion_procs\euc_jis_2004_and_shift_jis_2004\. Then similarly, move contents of D:\src\postgresql\src\backend\utils\mb\conversion_procs\utf8_and_shift_jis_2004\ to D:\src\postgresql\postgresql-8.3.7\src\backend\utils\mb\conversion_procs\utf8_and_shift_jis_2004\.

That’s it, now we can compile. Start the MSYS console and run the following commands

cd /d/src/postgresql/postgresql-8.3.7
./configure --prefix=/c/postgres && make && make install

After successful compilation, PostgreSQL will be located in C:\postgres.

Building GEOS

GEOS (Geometry Engine — Open Source) is a C++ port of the Java Topology Suite (JTS). GEOS provides access to JTS functionality using C++. This includes spatial function operators defined by the OpenGIS Simple Features specification for SQL, as well as specific functions for working with JTS topology. GEOS is required by PostG

Download the latest version of GEOS from its homepage, and unzip the archive to the source directory. In the MSYS console navigate to the created directory and use the following commands to build it

cd /d/src/geos-3.1.0
configure --prefix=/c/postgres && make && make install

If necessary, you can remove debugging information from the compiled library with the command

strip /c/postgres/bin/libgeos-3-1-1.dll

Building Proj

Proj is a generic library for coordinate transformations. It is the de facto standard for coordinate and projection transformations. Along with GEOS, it is a required dependency for PostGIS.

Download the latest version of Proj from its website. Once the download is complete, we perform the familiar sequence of actions: extract the files from the archive, enter the directory containing the source code, and compile

cd /d/src/proj-4.6.1
configure --prefix=/c/postgres && make && make install

Building PostGIS

We will build the latest SVN version, and since our goal is to test WKTRaster, we won’t build a GUI for shp2pgsql.

Download the source code archive, unpack it to the source directory, open the MSYS console and run the following commands

cd /d/src/postgis-1.4.0SVN
configure \
  --prefix=/c/postgres \
  --with-pgconfig=/c/postgres/bin/pg_config \
  --with-geosconfig=/c/postgres/bin/geos-config \
  --with-projdir=/c/postgres
make && make install

Building WKTRaster

Download the archive with the latest svn version of WKTRaster, extract it to D:\src\wktraster-0.1.6SVN, open the MSYS console and run the following commands

cd /d/src/wktraster-0.1.6SVN
./configure \
  --with-postgis-sources=/d/src/postgis-1.4.0SVN \
  --with-pgconfig=/c/postgres/bin/pg_config \
  --with-geosconfig=/c/postgres/bin/geos-config
make && make install

Setup

The hardest part is over, we just need to copy all dll files from the C:\postgres\lib directory to C:\postgres\bin, add C:\postgres\bin to the PATH variable and we are ready to configure PostgreSQL server.

First of all, it is necessary to create a user under whose name the PostgreSQL server will be started. Right-click on the ‘My Computer’ icon, select ‘Manage’ from the context menu, then open the ‘System Tools → Local Users and Groups → Users’ section. Create a postgres user and set a password for it.

Open a Windows command line: go to the “Start” menu and select “Run”, a small window will appear where we need to enter the following text and press the “OK” button

runas /user:postgres cmd.exe

This will open a Windows console, and all further commands should be entered and executed in this console window.

rem initialize the database directory (specify the encoding and path at your discretion)
initdb -E win1251 -D:\pg_data
rem start the server, after the start another console window will be open, ignore it
postmaster -D:\pg_data
rem create postgis database
createdb -U postgres postgis
rem enable PL/pgSQL procedural language support
createlang plpgsql -U postgres -D:\pg_data
rem load functions and objects necessary for working with spatial data
psql -a c:\postgres\share\contrib\postgis.sql -U postgres -d postgis
rem load coordinate system descriptions
psql -a c:\postgres\share\contrib\spatial_ref_sys.sql -U postgres -d postgis
rem load functions and objects necessary for working with raster data
psql -a c:\postgres\share\contrib\rtpostgis.sql -U postgres -d postgis

To stop the PostgreSQL server run the following command as the postgres user

pg_ctl.exe stop -D D:\pg_data

Starting and stopping the PostgreSQL server manually is fine if you don’t need it all the time, but it is much more convenient to have it running as a service in the background. This can be easily achieved with the following steps:

  1. stop the server if it is running
    pg_ctl.exe stop -D D:\pg_data
    
  2. register new service
    pg_ctl.exe register -N "PostgreSQL_8.3.7" -U postgres -D:\pg_data
    
  3. right-click on the “My Computer” icon, select “Manage”, open the “Services and Applications → Services” section, double-click on the name of the newly created service to open its properties, and go to the “Log On” tab
  4. set the user name .\postgres for the service
  5. specify the correct password in the password input window, click “Apply”.
  6. get a message that the user postgres is allowed to log in as a service, close the properties window and start the service

That’s it. We have successfully compiled PostgreSQL, PostGIS and WKTRaster, created a database cluster and configured PostgreSQL to run as a service.

The MSYS/MinGW environment created during the build process can be used in the future, for example, to upgrade our setup or build other software. If you don’t plan to build anything else or update your setup when new versions of the software will be released, you can safely delete the D:\src, c:\MinGW and C:\MSYS directories, as they are no longer needed for the operation of the components we have built.

⮜ Prev
Next ⮞