PostGIS - PostgreSQL GIS Extension Installation

Posted in: Data  
Tags: PostgreSQL   PostGIS   Geospation   Installation  
Author: Bambang Purnomosidi D. P. | | 3 minutes reading time

PostgreSQL - in its default installation - geometric data types for spatial object. More geospatial features exist in PostgreSQL extension - PostGIS. Here’s hos to insatll PostGIS extension.

Prerequisites

PostGIS needs GEOS and SFCGAL. In Debian-based system, these packages are needed:

  1. gdal-data and libgdal-dev.
  2. libgeosVERRSION and libgeos-dev.
  3. libsfcgal1 and libsfcgal-dev.
  4. protobuf-* and libprotobuf-c-dev
  5. dblatex if you want to build PostGIS documentation

Build

Get the source code, extract:

$ tar -xvf postgis-3.4.0.tar.gz
$ cd postgis-3.4.0
$ ./configure --prefix=/home/bpdp/software/dbms/postgis-3.4.0/

The result, more or less will be something like this in the end:

...
...

  PostGIS is now configured for x86_64-pc-linux-gnu

 -------------- Compiler Info ------------- 
  C compiler:           gcc -std=gnu99 -g -O2 -fno-math-errno -fno-signed-zeros -Wall
  C++ compiler (Wagyu): gcc -std=c++11 -x c++ 
  C++ compiler (FlatGeobuf): gcc -std=c++11 -x c++ 
  CPPFLAGS:              -I/usr/include -I/usr/include/x86_64-linux-gnu   -I/usr/include/libxml2 -I/usr/include -I/usr/include/json-c   -DNDEBUG 
  LDFLAGS:               -lm
  SQL preprocessor:     /usr/bin/cpp -traditional-cpp -w -P -Upixel -Ubool
  Archiver:             gcc-ar rs

 -------------- Additional Info ------------- 
  Interrupt Tests:   ENABLED

 -------------- Dependencies -------------- 
  GEOS config:          /usr/bin/geos-config
  GEOS version:         3.12.0
  GDAL config:          /usr/bin/gdal-config
  GDAL version:         3.7.1
  SFCGAL config:        /usr/bin/sfcgal-config
  SFCGAL version:       1.4.1
  PostgreSQL config:    /home/bpdp/software/dbms/postgresql-15/bin/pg_config
  PostgreSQL version:   PostgreSQL 15.4
  PROJ4 version:        92
  Libxml2 config:       /usr/bin/xml2-config
  Libxml2 version:      2.9.14
  JSON-C support:       yes
  protobuf support:     yes
  protobuf-c version:   1004001
  PCRE support:         Version 2
  Perl:                 /usr/bin/perl

 --------------- Extensions --------------- 
  PostgreSQL EXTENSION support:       enabled
  PostGIS Raster:                     enabled
  PostGIS Topology:                   enabled
  SFCGAL support:                     enabled
  Address Standardizer support:       enabled

 -------- Documentation Generation -------- 
  xsltproc:             /usr/bin/xsltproc
  xsl style sheets:     /usr/share/xml/docbook/stylesheet/docbook-xsl
  dblatex:              /usr/bin/dblatex
  convert:              /usr/bin/convert
  mathml2.dtd:          http://www.w3.org/Math/DTD/mathml2/mathml2.dtd
$

So now we are ready to build and install:

$ make
...
...
$ make install
...
...
'/home/bpdp/software/dbms/postgresql-15.4/share/extension/address_standardizer--3.3.1--ANY.sql' -> 'address_standardizer--TEMPLATED--TO--ANY.sql'
'/home/bpdp/software/dbms/postgresql-15.4/share/extension/address_standardizer--3.3.2--ANY.sql' -> 'address_standardizer--TEMPLATED--TO--ANY.sql'
'/home/bpdp/software/dbms/postgresql-15.4/share/extension/address_standardizer--3.3.3--ANY.sql' -> 'address_standardizer--TEMPLATED--TO--ANY.sql'
'/home/bpdp/software/dbms/postgresql-15.4/share/extension/address_standardizer--3.3.4--ANY.sql' -> 'address_standardizer--TEMPLATED--TO--ANY.sql'
make[3]: Leaving directory '/home/bpdp/master/postgresql/15/postgis-3.4.0/extensions/address_standardizer'
make[2]: Leaving directory '/home/bpdp/master/postgresql/15/postgis-3.4.0/extensions'
make[1]: Leaving directory '/home/bpdp/master/postgresql/15/postgis-3.4.0'
$

The results will be installed in PostgreSQL location and also at the –prefix location above. The –prefix location is used for some tools developed by PostGIS project:

$ pwd
/home/bpdp/software/dbms/postgis-3.4.0
$ tree .
.
+-- bin
|�� +-- pgsql2shp
|�� +-- pgtopo_export
|�� +-- pgtopo_import
|�� +-- postgis
|�� +-- postgis_restore
|�� +-- raster2pgsql
|�� +-- shp2pgsql
+-- share
    +-- man
        +-- man1
            +-- pgsql2shp.1
            +-- pgtopo_export.1
            +-- pgtopo_import.1
            +-- postgis.1
            +-- postgis_restore.1
            +-- shp2pgsql.1

5 directories, 13 files
$

Use PostGIS

Put the bin location above into PATH and share/man into MANPATH. Now we can try to use PostGIS:

$ psql -U bpdp -d template1
psql (15.4)
Type "help" for help.

template1=# CREATE^C
template1=# CREATE EXTENSION postgis;
CREATE EXTENSION
template1=# SELECT PostGIS_Full_Version();
                                                                                                                                                postg
is_full_version                                                                                                                                      
           
-----------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------
-----------
 POSTGIS="3.4.0 0874ea3" [EXTENSION] PGSQL="150" GEOS="3.12.0-CAPI-1.18.0" PROJ="9.2.1 NETWORK_ENABLED=OFF URL_ENDPOINT=https://cdn.proj.org USER_WRI
TABLE_DIRECTORY=/home/bpdp/.local/share/proj DATABASE_PATH=/usr/share/proj/proj.db" LIBXML="2.9.14" LIBJSON="0.17" LIBPROTOBUF="1.4.1" WAGYU="0.5.0 (
Internal)"
(1 row)

template1=# SELECT * FROM pg_available_extensions WHERE name = 'postgis';
  name   | default_version | installed_version |                          comment                           
---------+-----------------+-------------------+------------------------------------------------------------
 postgis | 3.4.0           | 3.4.0             | PostGIS geometry and geography spatial types and functions
(1 row)

template1=#