# PostGIS Installation

This chapter details the steps required to install PostGIS.

## Short Version

To compile assuming you have all the dependencies in your search path:

    tar -xvzf postgis-&last_release_version;.tar.gz
    cd postgis-&last_release_version;
    ./configure
    make
    make install

Once PostGIS is installed, it needs to be enabled (create_spatial_db) or upgraded (upgrading) in each individual database you want to use it in.

## Compiling and Install from Source

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.

This section includes general compilation instructions, if you are compiling for Windows etc or another OS, you may find additional more detailed help at PostGIS User contributed compile guides and PostGIS Dev Wiki.

Pre-Built Packages for various OS are listed in PostGIS Pre-built Packages

If you are a windows user, you can get stable builds via Stackbuilder or PostGIS Windows download site We also have very bleeding-edge windows experimental builds that are built usually once or twice a week or whenever anything exciting happens. You can use these to experiment with the in progress releases of PostGIS

The PostGIS module is an extension to the PostgreSQL backend server. As such, PostGIS &last_release_version; requires full PostgreSQL server headers access in order to compile. It can be built against PostgreSQL versions &min_postgres_version; - &max_postgres_version;. Earlier versions of PostgreSQL are not supported.

Refer to the PostgreSQL installation guides if you haven't already installed PostgreSQL. https://www.postgresql.org .

For GEOS functionality, when you install PostgreSQL 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.

### Getting the Source

Retrieve the PostGIS source archive from the downloads website &postgis_download_url;

    wget &postgis_download_url;
    tar -xvzf postgis-&last_release_version;.tar.gz
    cd postgis-&last_release_version;

This will create a directory called `postgis-&last_release_version;` in the current working directory.

Alternatively, checkout the source from the git repository https://git.osgeo.org/gitea/postgis/postgis/ .

    git clone https://git.osgeo.org/gitea/postgis/postgis.git postgis
    cd postgis
    sh autogen.sh

Change into the newly created `postgis` directory to continue the installation.

    ./configure

### Install Requirements

PostGIS has the following requirements for building and usage:

Required

- PostgreSQL &min_postgres_version; - &max_postgres_version;. A complete installation of PostgreSQL (including server headers) is required. PostgreSQL is available from https://www.postgresql.org 18 .

  For current release-line support and end-of-life policy, see https://postgis.net/development/versions_eol/

- GNU C compiler (`gcc`). Some other ANSI C compilers can be used to compile PostGIS, but we find far fewer problems when compiling with `gcc`.

- GNU Make (`gmake` or `make`). For many systems, GNU `make` is the default version of make. Check the version by invoking `make -v`. Other versions of `make` may not process the PostGIS `Makefile` properly.

- Proj reprojection library. Proj 6.1 or above is required. The Proj library is used to provide coordinate reprojection support within PostGIS. Proj is available for download from https://proj.org/ .

- GEOS geometry library, version &min_geos_version; or greater, but GEOS 3.14+ is required to take full advantage of all the new functions and features. GEOS is available for download from https://libgeos.org .

- LibXML2, version 2.5.x or higher. LibXML2 is currently used in some imports functions (ST_GeomFromGML and ST_GeomFromKML). LibXML2 is available for download from https://gitlab.gnome.org/GNOME/libxml2/-/releases.

- JSON-C, version 0.9 or higher. JSON-C is currently used to import GeoJSON via the function ST_GeomFromGeoJson. JSON-C is available for download from https://github.com/json-c/json-c/releases/.

- The GNU Multiple Precision Arithmetic Library (libgmp). Used to control precision of math operations in PostGIS Topology. GMP is available for download from https://gmplib.org/.

- GDAL, version 3+ is preferred. This is required for raster support. https://gdal.org/download.html.

- If compiling with PostgreSQL+JIT, LLVM version >=6 is required https://trac.osgeo.org/postgis/ticket/4125.

Optional

- GDAL (pseudo optional) only if you don't want raster you can leave it out. Also make sure to enable the drivers you want to use as described in raster_configuration.

- GTK (requires GTK+2.0, 2.8+) to compile the shp2pgsql-gui shape file loader. http://www.gtk.org/ .

- SFCGAL, 1.4.1 or higher is required, 2.3+ is needed to be able to use all functionality. SFCGAL can be used to provide additional 2D and 3D advanced analysis functions to PostGIS cf reference_sfcgal. https://sfcgal.gitlab.io/sfcgal/.

- To enable ST_AsMVT protobuf-c library 1.1.0 or higher (for usage) and the protoc-c compiler (for building) are required. Also, pkg-config is required to verify the correct minimum version of protobuf-c. See protobuf-c. By default, Postgis will use Wagyu to validate MVT polygons faster which requires a c++11 compiler. It will use CXXFLAGS and the same compiler as the PostgreSQL installation. To disable this and use GEOS instead use the `--without-wagyu` during the configure step.

- CUnit (`CUnit`). This is needed for regression testing. http://cunit.sourceforge.net/

- DocBook (`xsltproc`) is required for building the documentation. Docbook is available from http://www.docbook.org/ .

- DBLatex (`dblatex`) is required for building the documentation in PDF format. DBLatex is available from http://dblatex.sourceforge.net/ .

- PDF documentation needs an SVG rasterizer for its high-resolution figure fallbacks. The build prefers `rsvg-convert`, and can also use GraphicsMagick (`gm convert`) or ImageMagick (`magick` or `convert`).

### Build configuration

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

    ./configure

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.

`--with-library-minor-version`

- Starting with PostGIS 3.0, the library files generated by default will no longer have the minor version as part of the file name. This means all PostGIS 3 libs will end in `postgis-3`. This was done to make pg_upgrade easier, with downside that you can only install one version PostGIS 3 series in your server. To get the old behavior of file including the minor version: e.g. `postgis-3.0` add this switch to your configure statement.

`--prefix=PREFIX`

- This is the installation prefix for PostGIS command-line utilities. By default, utilities are installed in the executable directory reported by the detected PostgreSQL installation.

  PostgreSQL extension files are installed in the directories reported by `pg_config`, so the server can load them. This includes the PostGIS shared library and SQL extension files. Use `--with-pgconfig=FILE` to choose the PostgreSQL installation that PostGIS builds and installs against.

`--with-pgconfig=FILE`

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

`--with-gdalconfig=FILE`

- GDAL, a required library, provides functionality needed for raster support `gdal-config` to enable software installations to locate the GDAL installation directory. Use this parameter (`--with-gdalconfig=/path/to/gdal-config`) to manually specify a particular GDAL installation that PostGIS will build against.

`--with-geosconfig=FILE`

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

`--with-xml2config=FILE`

- LibXML is the library required for doing GeomFromKML/GML processes. It normally is found if you have libxml installed, but if not or you want a specific version used, you'll need to point PostGIS at a specific `xml2-config` confi file to enable software installations to locate the LibXML installation directory. Use this parameter (`>--with-xml2config=/path/to/xml2-config`) to manually specify a particular LibXML installation that PostGIS will build against.

`--with-projdir=DIR`

- Proj is a reprojection library required by PostGIS. Use this parameter (`--with-projdir=/path/to/projdir`) to manually specify a particular Proj installation directory that PostGIS will build against.

`--with-libiconv=DIR`

- Directory where iconv is installed.

`--with-jsondir=DIR`

- JSON-C is an MIT-licensed JSON library required by PostGIS ST_GeomFromJSON support. Use this parameter (`--with-jsondir=/path/to/jsondir`) to manually specify a particular JSON-C installation directory that PostGIS will build against.

`--with-gui`

- Compile the data import GUI (requires GTK+2.0). This will create shp2pgsql-gui graphical interface to shp2pgsql.

`--without-raster`

- Compile without raster support.

`--without-topology`

- Compile without topology support.

`--with-gettext=no`

- By default PostGIS will try to detect gettext support and compile with it, however if you run into incompatibility issues that cause breakage of loader, you can disable it entirely with this command. Refer to ticket http://trac.osgeo.org/postgis/ticket/748 for an example issue solved by configuring with this. NOTE: that you aren't missing much by turning this off. This is used for international help/label support for the GUI loader which is not yet documented and still experimental.

`--with-sfcgal=PATH`

- By default PostGIS will not install with sfcgal support without this switch. `PATH` is an optional argument that allows to specify an alternate PATH to sfcgal-config.

`--without-phony-revision`

- Disable updating postgis_revision.h to match current HEAD of the git repository.

If you obtained PostGIS from the code repository , the first step is really to run the script

    ./autogen.sh

This script will generate the `configure` script that in turn is used to customize the installation of PostGIS.

If you instead obtained PostGIS as a tarball, running `./autogen.sh` is not necessary as `configure` has already been generated.

### Building

Once the Makefile has been generated, building PostGIS is as simple as running

    make

    [last line of output]
    PostGIS was built successfully. Ready to install.

All the functions have comments generated from the documentation. If you wish to install these comments into your spatial databases later, run the command which requires docbook. The postgis_comments.sql and other package comment files are also packaged in the tar.gz distribution in the doc folder so no need to make comments if installing from the tar ball. Comments are also included as part of the CREATE EXTENSION install.

    make comments

The `make cheatsheets` target generates html cheat sheets suitable for quick reference or for student handouts. This requires xsltproc to build and will generate 4 files in doc folder `topology_cheatsheet.html`, `raster_cheatsheet.html`, `postgis_cheatsheet.html`

You can download some pre-built ones available in html and pdf from PostGIS / PostgreSQL Study Guides

    make cheatsheets

### Building PostGIS Extensions and Deploying them

The PostGIS extensions are built and installed automatically when PostgreSQL extension support is available.

If you are building from source repository, you need to build the function descriptions first. These get built if you have docbook installed. You can also manually build with the statement:

    make comments

Building the comments is not necessary if you are building from a release tar ball since these are packaged pre-built with the tar ball already.

The extensions should automatically build as part of the make install process. You can if needed build from the extensions folders or copy files if you need them on a different server.

Set `PGUSER` if you need to override the psql connection user, then test before installing. After installation, run the extension regression tests.

    cd extensions
    cd postgis
    make clean
    make
    export PGUSER=postgres
    make check
    make install
    make check RUNTESTFLAGS=--extension

`make check` uses psql to run tests and as such can use psql environment variables. Common ones useful to override are `PGUSER`,`PGPORT`, and `PGHOST`. Refer to psql environment variables

The extension files will always be the same for the same version of PostGIS and PostgreSQL regardless of OS, so it is fine to copy over the extension files from one OS to another as long as you have the PostGIS binaries already installed on your servers.

If you want to install the extensions manually on a separate server different from your development, You need to copy the following files from the extensions folder into the `PostgreSQL / share / extension` folder of your PostgreSQL install as well as the needed binaries for regular PostGIS if you don't have them already on the server.

- These are the control files that denote information such as the version of the extension to install if not specified. `postgis.control, postgis_topology.control`.

- All the files in the /sql folder of each extension. Note that these need to be copied to the root of the PostgreSQL share/extension folder `extensions/postgis/sql/*.sql`, `extensions/postgis_topology/sql/*.sql`

Once you do that, you should see `postgis`, `postgis_topology` as available extensions in PgAdmin -> extensions.

If you are using psql, you can verify that the extensions are installed by running this query:

    SELECT name, default_version, installed_version
    FROM pg_available_extensions
    WHERE name LIKE 'postgis%'
    ORDER BY name;

    ┌──────────────────┬─────────────────┬───────────────────┐
    │ name             │ default_version │ installed_version │
    ├──────────────────┼─────────────────┼───────────────────┤
    │ postgis          │ &last_release_version;        │ &last_release_version;        │
    │ postgis_raster   │ &last_release_version;        │ &last_release_version;        │
    │ postgis_sfcgal   │ &last_release_version;        │                   │
    │ postgis_topology │ &last_release_version;        │                   │
    └──────────────────┴─────────────────┴───────────────────┘

If you have the extension installed in the database you are querying, you'll see mention in the `installed_version` column. If you get no records back, it means you don't have postgis extensions installed on the server at all. PgAdmin III 1.14+ will also provide this information in the `extensions` section of the database browser tree and will even allow upgrade or uninstall by right-clicking.

If you have the extensions available, you can install postgis extension in your database of choice by either using pgAdmin extension interface or running these sql commands:

    CREATE EXTENSION postgis;
    CREATE EXTENSION postgis_raster;
    CREATE EXTENSION postgis_sfcgal;
    CREATE EXTENSION postgis_topology;

In psql you can use to see what versions you have installed and also what schema they are installed.

    \connect mygisdb
    \x
    \dx postgis*

    List of installed extensions
    ┌─[ RECORD 1 ]────────────────────────────────────────────────┐
    │ Name        │ postgis                                       │
    │ Version     │ &last_release_version;                                        │
    │ Schema      │ public                                        │
    │ Description │ PostGIS geometry, geography, and raster…      │
    ├─[ RECORD 2 ]────────────────────────────────────────────────┤
    │ Name        │ postgis_raster                                │
    │ Version     │ &last_release_version;                                        │
    │ Schema      │ public                                        │
    │ Description │ PostGIS raster types and functions            │
    ├─[ RECORD 3 ]────────────────────────────────────────────────┤
    │ Name        │ postgis_topology                              │
    │ Version     │ &last_release_version;                                        │
    │ Schema      │ topology                                      │
    │ Description │ PostGIS topology spatial types and functions  │
    └─────────────────────────────────────────────────────────────┘

Extension tables `spatial_ref_sys`, `layer`, `topology` can not be explicitly backed up. They can only be backed up when the respective `postgis` or `postgis_topology` extension is backed up, which only happens when you back up the whole database. Only srid records not packaged with PostGIS are captured in backups, so don't change the entries we ship and expect the modifications to persist. Put in a ticket if you find an issue. The structures of extension tables are never backed up since they are created with `CREATE EXTENSION` and assumed to be the same for a given version of an extension. These behaviors are built into the current PostgreSQL extension model.

If you installed &last_release_version;, without using our wonderful extension system, you can change it to be extension based by running the below commands to package the functions in their respective extension. Installing using `unpackaged` was removed in PostgreSQL 13, so you are advised to switch to an extension build before upgrading to PostgreSQL 13.

    CREATE EXTENSION postgis FROM unpackaged;
    CREATE EXTENSION postgis_raster FROM unpackaged;
    CREATE EXTENSION postgis_topology FROM unpackaged;

### Testing

If you wish to test the PostGIS build, run

    make check

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 Proj locations, you may need to add their library locations to the `LD_LIBRARY_PATH` environment variable.

Currently, the `make check` relies on the `PATH` and `PGPORT` environment variables when performing the checks - it does not use the PostgreSQL version that may have been specified using the configuration parameter `--with-pgconfig`. So make sure to modify your PATH to match the detected PostgreSQL installation during configuration or be prepared to deal with the impending headaches.

Sandboxed build accounts that are not PostgreSQL superusers can delegate database ownership during the regression cycle by exporting `POSTGIS_REGRESS_DB_OWNER`. The harness will create the temporary regression database owned by the nominated role while continuing to connect using the less privileged account. Combine this with `POSTGIS_REGRESS_ROLE_EXT_CREATOR` when the extension creation role must differ from the database owner.

These variables allow automated environments to exercise the full upgrade and extension install paths without promoting the calling account to superuser, provided the target PostgreSQL instance permits extension installation by those delegate roles.

If successful, make check will produce the output of almost 500 tests. The results will look similar to the following (numerous lines omitted below):

         CUnit - A unit testing framework for C - Version 2.1-3
         http://cunit.sourceforge.net/

      .
      .
      .

    Run Summary:    Type  Total    Ran Passed Failed Inactive
                  suites     44     44    n/a      0        0
                   tests    300    300    300      0        0
                 asserts   4215   4215   4215      0      n/a
    Elapsed time =    0.229 seconds

      .
      .
      .

    Running tests

      .
      .
      .

    Run tests: 134
    Failed: 0

    -- if you build with SFCGAL

      .
      .
      .

    Running tests

      .
      .
      .

    Run tests: 13
    Failed: 0

    -- if you built with raster support

      .
      .
      .

    Run Summary:    Type  Total    Ran Passed Failed Inactive
                  suites     12     12    n/a      0        0
                   tests     65     65     65      0        0
                 asserts  45896  45896  45896      0      n/a

      .
      .
      .

    Running tests

      .
      .
      .

    Run tests: 101
    Failed: 0

    -- topology regress

    .
    .
    .

    Running tests

      .
      .
      .

    Run tests: 51
    Failed: 0

    -- if you built --with-gui, you should see this too

         CUnit - A unit testing framework for C - Version 2.1-2
         http://cunit.sourceforge.net/

      .
      .
      .

    Run Summary:    Type  Total    Ran Passed Failed Inactive
                  suites      2      2    n/a      0        0
                   tests      4      4      4      0        0
                 asserts      4      4      4      0      n/a

output should look like:

    ============== dropping database "contrib_regression" ==============
    DROP DATABASE
    ============== creating database "contrib_regression" ==============
    CREATE DATABASE
    ALTER DATABASE
    ============== installing fuzzystrmatch               ==============
    CREATE EXTENSION
    ============== installing postgis                     ==============
    CREATE EXTENSION
    ============== running regression test queries        ==============
    test test-normalize_address   ... ok

    =====================
    All 2 tests passed.
    =====================

### Installation

To install PostGIS, type

    make install

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 `[prefix]/bin`.

- The SQL files, such as `postgis.sql`, are installed in `[prefix]/share/contrib`.

- The PostGIS libraries are installed in `[prefix]/lib`.

If you previously ran the `make comments` command to generate the `postgis_comments.sql`, `raster_comments.sql` file, install the sql file by running

    make comments-install

`postgis_comments.sql`, `raster_comments.sql`, `topology_comments.sql` was separated from the typical build and installation targets since with it comes the extra dependency of `xsltproc`.

## Common Problems during installation

There are several things to check when your installation or upgrade doesn't go as you expected.

PostgreSQL version mismatch.

Check that you have installed PostgreSQL &min_postgres_version; 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 &min_postgres_version; 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:

    SELECT version();

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

Restoring an upgrade into a database without PostGIS.

If your upgrade fails, make sure you are restoring into a database that already has PostGIS installed.

    SELECT PostGIS_Full_Version();

Also check that configure has correctly detected the location and version of PostgreSQL, the Proj library and the GEOS library.

The output from configure is used to generate the `postgis_config.h` file. Check that the `POSTGIS_PGSQL_VERSION`, `POSTGIS_PROJ_VERSION` and `POSTGIS_GEOS_VERSION` variables have been set correctly.