Table of Contents
This chapter details the steps required to install PostGIS.
tar xvfz postgis-1.4.3SVN.tar.gz cd postgis-1.4.3SVN ./configure make make install createdb yourdatabase createlang plpgsql yourdatabase psql -d yourdatabase -f postgis.sql psql -d yourdatabase -f postgis_comments.sql psql -d yourdatabase -f spatial_ref_sys.sql
The rest of this chapter goes into detail each of the above installation steps.
PostGIS has the following requirements for building and usage:
PostgreSQL 8.2 - PostgreSQL 8.4. A complete installation of PostgreSQL (including server headers) is required. PostgreSQL is available from http://www.postgresql.org .
For a full PostgreSQL / PostGIS support matrix and PostGIS/GEOS support matrix refer to http://trac.osgeo.org/postgis/wiki/UsersWikiPostgreSQLPostGIS
GNU C compiler (
gcc). Some other ANSI C compilers
can be used to compile PostGIS, but we find far fewer problems when
GNU Make (
For many systems, GNU
make is the default version
of make. Check the version by invoking
Other versions of
make may not process the
Proj4 reprojection library, version 4.5.0 or greater. The Proj4 library is used to provide coordinate reprojection support within PostGIS. Proj4 is available for download from http://trac.osgeo.org/proj/ .
GEOS geometry library, version 3.0.0 or greater. The GEOS library is used to provide geometry tests (ST_Touches(), ST_Contains(), ST_Intersects()) and operations (ST_Buffer(), ST_Union(), ST_Difference()) within PostGIS. GEOS is available for download from http://trac.osgeo.org/geos/ .
Apache Ant (
ant) is required for building any of
the drivers under the
java directory. Ant is
xsltproc) is required for building the
documentation. Docbook is available from
dblatex) is required for building the
documentation in PDF format. DBLatex is available from
convert) is required to generate the
images used in the documentation. ImageMagick is available from
Retrieve the PostGIS source archive from the downloads website http://postgis.refractions.net/download/postgis-1.4.3SVN.tar.gz
wget http://postgis.refractions.net/download/postgis-1.4.3SVN.tar.gz tar -xvzf postgis-1.4.3SVN.tar.gz
This will create a directory called
postgis-1.4.3SVN in the current working
svn checkout http://svn.osgeo.org/postgis/trunk/ postgis-1.4.3SVN
Change into the newly created
postgis-1.4.3SVN directory to continue
Many OS systems now include pre-built packages for PostgreSQL/PostGIS. In many cases compilation is only necessary if you want the most bleeding edge versions or you are a package maintainer.
The PostGIS module is an extension to the PostgreSQL backend server. As such, PostGIS 1.4.3SVN requires full PostgreSQL server headers access in order to compile. It can be built against PostgreSQL versions 8.2.0 or higher. Earlier versions of PostgreSQL are not supported.
Refer to the PostgreSQL installation guides if you haven't already installed PostgreSQL. http://www.postgresql.org .
For GEOS functionality, when you install PostgresSQL you may need to explicitly link PostgreSQL against the standard C++ library:
LDFLAGS=-lstdc++ ./configure [YOUR OPTIONS HERE]
This is a workaround for bogus C++ exceptions interaction with older development tools. If you experience weird problems (backend unexpectedly closed or similar things) try this trick. This will require recompiling your PostgreSQL from scratch, of course.
The following steps outline the configuration and compilation of the PostGIS source. They are written for Linux users and will not work on Windows or Mac.
As with most linux installations, the first step is to generate the Makefile that will be used to build the source code. This is done by running the shell script
With no additional parameters, this command will attempt to automatically locate the required components and libraries needed to build the PostGIS source code on your system. Although this is the most common usage of ./configure, the script accepts several parameters for those who have the required libraries and programs in non-standard locations.
The following list shows only the most commonly used parameters. For a complete list, use the --help or --help=short parameters.
This is the location the PostGIS libraries and SQL scripts will be installed to. By default, this location is the same as the detected PostgreSQL installation.
This paramater is currently broken, as the package will only install into the PostgreSQL installation directory. Visit http://trac.osgeo.org/postgis/ticket/160 to track this bug.
PostgreSQL provides a utility called pg_config to enable extensions like PostGIS to locate the PostgreSQL installation directory. Use this parameter (--with-pgconfig=/path/to/pg_config) to manually specify a particular PostgreSQL installation that PostGIS will build against.
GEOS, a required geometry library, provides a utility called geos-config to enable software installations to locate the GEOS installation directory. Use this parameter (--with-geosconfig=/path/to/geos-config) to manually specify a particular GEOS installation that PostGIS will build against.
Proj4 is a reprojection library required by PostGIS. Use this parameter (--with-projdir=/path/to/projdir) to manually specify a particular Proj4 installation directory that PostGIS will build against.
Compile the data import GUI (requires GTK+2.0). This will create shp2pgsql-gui graphical interface to shp2pgsql.
If you obtained PostGIS from the SVN repository , the first step is really to run the script
This script will generate the configure script that in turn is used to customize the intallation of PostGIS.
If you instead obtained PostGIS as a tarball, running ./autogen.sh is not necessary as configure has already been generated.
Once the Makefile has been generated, building PostGIS is as simple as running
The last line of the output should be "
PostGIS was built
successfully. Ready to install."
As of PostGIS v1.4.0, all the functions have comments generated from the documentation. If you wish to install these comments into your spatial databases later, run the command
If you wish to test the PostGIS build, run
The above command will run through various checks and regression tests using the generated library against an actual PostgreSQL database.
If you configured PostGIS using non-standard PostgreSQL, GEOS, or Proj4 locations, you may need to add their library locations to the LD_LIBRARY_PATH environment variable.
Currently, the make check relies on the
If successful, the output of the test should be similiar to the following:
CUnit - A Unit testing framework for C - Version 2.1-0 http://cunit.sourceforge.net/ Suite: PostGIS Computational Geometry Suite Test: test_lw_segment_side() ... passed Test: test_lw_segment_intersects() ... passed Test: test_lwline_crossing_short_lines() ... passed Test: test_lwline_crossing_long_lines() ... passed Test: test_lwpoint_set_ordinate() ... passed Test: test_lwpoint_get_ordinate() ... passed Test: test_lwpoint_interpolate() ... passed Test: test_lwline_clip() ... passed Test: test_lwline_clip_big() ... passed Test: test_lwmline_clip() ... passed Test: test_geohash_point() ... passed Test: test_geohash_precision() ... passed Test: test_geohash() ... passed Suite: PostGIS Measures Suite Test: test_mindistance2d_recursive_tolerance() ... passed --Run Summary: Type Total Ran Passed Failed suites 2 2 n/a 0 tests 14 14 14 0 asserts 84 84 84 0 Creating spatial db postgis_reg TMPDIR is /tmp/pgis_reg_15328 PostgreSQL 8.3.7 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44) Postgis 1.4.0SVN - 2009-05-25 20:21:55 GEOS: 3.1.0-CAPI-1.5.0 PROJ: Rel. 4.6.1, 21 August 2008 Running tests loader/Point.............. ok loader/PointM.............. ok loader/PointZ.............. ok loader/MultiPoint.............. ok loader/MultiPointM.............. ok loader/MultiPointZ.............. ok loader/Arc.............. ok loader/ArcM.............. ok loader/ArcZ.......... ok loader/Polygon.............. ok loader/PolygonM.............. ok loader/PolygonZ.............. ok regress. ok regress_index. ok regress_index_nulls. ok lwgeom_regress. ok regress_lrs. ok removepoint. ok setpoint. ok simplify. ok snaptogrid. ok affine. ok wkt. ok measures. ok long_xact. ok ctors. ok sql-mm-serialize. ok sql-mm-circularstring. ok sql-mm-compoundcurve. ok sql-mm-curvepoly. ok sql-mm-general. ok sql-mm-multicurve. ok sql-mm-multisurface. ok geojson. ok gml. ok svg. ok kml. ok regress_ogc. ok regress_bdpoly. ok regress_proj. ok regress_ogc_cover. ok regress_ogc_prep. ok Run tests: 42 Failed: 0
To install PostGIS, type
This will copy the PostGIS installation files into their appropriate subdirectory specified by the --prefix configuration parameter. In particular:
The loader and dumper binaries are installed in
The SQL files, such as
The PostGIS libraries are installed in
If you previously ran the make comments command to
postgis_comments.sql file, install the
sql file by running
The first step in creating a PostGIS database is to create a simple PostgreSQL database.
Many of the PostGIS functions are written in the PL/pgSQL procedural language. As such, the next step to create a PostGIS database is to enable the PL/pgSQL language in your new database. This is accomplish by the command
createlang plpgsql [yourdatabase]
Now load the PostGIS object and function definitions into your database by
postgis.sql definitions file (located in
[prefix]/share/contrib as specified during the
psql -d [yourdatabase] -f postgis.sql
For a complete set of EPSG coordinate system definition identifiers, you
can also load the
file and populate the
spatial_ref_sys table. This will
permit you to perform ST_Transform() operations on geometries.
psql -d [yourdatabase] -f spatial_ref_sys.sql
If you wish to add comments to the PostGIS functions, the final step is to
postgis_comments.sql into your spatial
database. The comments can be viewed by simply typing \dd
[function_name] from a psql terminal window.
psql -d [yourdatabase] -f postgis_comments.sql
Some packaged distributions of PostGIS (in particular the Win32 installers
for PostGIS >= 1.1.5) load the PostGIS functions into a template
template_postgis. If the
template_postgis database exists in your PostgreSQL
installation then it is possible for users and/or applications to create
spatially-enabled databases using a single command. Note that in both
cases, the database user must have been granted the privilege to create
From the shell:
# createdb -T template_postgis my_spatial_db
postgres=# CREATE DATABASE my_spatial_db TEMPLATE=template_postgis
Upgrading existing spatial databases can be tricky as it requires replacement or introduction of new PostGIS object definitions.
Unfortunately not all definitions can be easily replaced in a live database, so sometimes your best bet is a dump/reload process.
PostGIS provides a SOFT UPGRADE procedure for minor or bugfix releases, and an HARD UPGRADE procedure for major releases.
Before attempting to upgrade postgis, it is always worth to backup your data. If you use the -Fc flag to pg_dump you will always be able to restore the dump with an HARD UPGRADE.
Soft upgrade consists of sourcing the postgis_upgrade.sql script in your spatial database:
$ psql -f postgis_upgrade.sql -d your_spatial_database
If a soft upgrade is not possible the script will abort and you will be warned about HARD UPGRADE being required, so do not hesitate to try a soft upgrade first.
If you can't find the
$ utils/postgis_proc_upgrade.pl postgis.sql > postgis_upgrade.sql
By HARD UPGRADE we intend full dump/reload of postgis-enabled databases. You need an HARD UPGRADE when postgis objects' internal storage changes or when SOFT UPGRADE is not possible. The Release Notes appendix reports for each version whether you need a dump/reload (HARD UPGRADE) to upgrade.
PostGIS provides an utility script to restore a dump produced with the pg_dump -Fc command. It is experimental so redirecting its output to a file will help in case of problems. The procedure is as follow:
Create a "custom-format" dump of the database you want to upgrade (let's call it "olddb")
$ pg_dump -Fc olddb > olddb.dump
Restore the dump contextually upgrading postgis into a new database. The new database doesn't have to exist. postgis_restore accepts createdb parameters after the dump file name, and that can for instance be used if you are using a non-default character encoding for your database. Let's call it "newdb", with UNICODE as the character encoding:
$ sh utils/postgis_restore.pl postgis.sql newdb olddb.dump -E=UNICODE > restore.log
Check that all restored dump objects really had to be restored from dump and do not conflict with the ones defined in postgis.sql
$ grep ^KEEPING restore.log | less
If upgrading from PostgreSQL < 8.0 to >= 8.0 you might want to drop the attrelid, varattnum and stats columns in the geometry_columns table, which are no-more needed. Keeping them won't hurt. DROPPING THEM WHEN REALLY NEEDED WILL DO HURT !
$ psql newdb -c "ALTER TABLE geometry_columns DROP attrelid" $ psql newdb -c "ALTER TABLE geometry_columns DROP varattnum" $ psql newdb -c "ALTER TABLE geometry_columns DROP stats"
spatial_ref_sys table is restore from the dump, to ensure your custom additions are kept, but the distributed one might contain modification so you should backup your entries, drop the table and source the new one. If you did make additions we assume you know how to backup them before upgrading the table. Replace of it with the new one is done like this:
$ psql newdb newdb=> truncate spatial_ref_sys; TRUNCATE newdb=> \i spatial_ref_sys.sql
There are several things to check when your installation or upgrade doesn't go as you expected.
Check that you you have installed PostgreSQL 8.1 or newer, and that you are compiling against the same version of the PostgreSQL source as the version of PostgreSQL that is running. Mix-ups can occur when your (Linux) distribution has already installed PostgreSQL, or you have otherwise installed PostgreSQL before and forgotten about it. PostGIS will only work with PostgreSQL 8.1 or newer, and strange, unexpected error messages will result if you use an older version. To check the version of PostgreSQL which is running, connect to the database using psql and run this query:
If you are running an RPM based distribution, you can check for the existence of pre-installed packages using the rpm command as follows: rpm -qa | grep postgresql
Also check that configure has correctly detected the location and version of PostgreSQL, the Proj4 library and the GEOS library.
The output from configure is used to generate the
postgis_config.h file. Check that the
POSTGIS_GEOS_VERSION variables have been set
The JDBC extensions provide Java objects corresponding to the internal PostGIS types. These objects can be used to write Java clients which query the PostGIS database and draw or do calculations on the GIS data in PostGIS.
java/jdbc sub-directory of the PostGIS
ant command. Copy the
postgis.jar file to wherever you keep your java
The JDBC extensions require a PostgreSQL JDBC driver to be present in the current CLASSPATH during the build process. If the PostgreSQL JDBC driver is located elsewhere, you may pass the location of the JDBC driver JAR separately using the -D parameter like this:
# ant -Dclasspath=/path/to/postgresql-jdbc.jar
PostgreSQL JDBC drivers can be downloaded from http://jdbc.postgresql.org .
The data loader and dumper are built and installed automatically as part of the PostGIS build. To build and install them manually:
# cd postgis-1.4.3SVN/loader # make # make install
The loader is called
shp2pgsql and converts ESRI
Shape files into SQL suitable for loading in PostGIS/PostgreSQL. The
dumper is called
pgsql2shp and converts PostGIS
tables (or queries) into ESRI Shape files. For more verbose documentation,
see the online help, and the manual pages.