PostGIS is a spatial database extender for PostgreSQL object-relational database. It adds support for geographic objects allowing location queries to be run in SQL. In effect, PostGIS “spatially enables” the PostgreSQL server, allowing it to be used as a backend spatial database for geographic information systems (GIS).
PostGIS is a free open source project, licensed under GNU GPLv2.
In this article, we will discuss about how to install PostGIS on Slackware64. For this purpose, we will use following materials:
- Slackware64 14.0
- PostgreSQL 9.3.5
- GEOS 3.4.2
- GDAL 1.11
- Proj 4.8.0
- JSON-C 0.11
- PostGIS 2.1.3
PostgreSQL is a DBMS which can be freely downloaded from PostgreSQL. Or download the latest version 9.35 here.
GEOS is Geometry Engine – Open Source, a C++ post of the Java Topology Suite (JTS). To download it, go to GEOS page or download GEOS 3.4.2 from here.
GDAL is Geospatial Data Abstraction Library. To download it, go to GDAL page or download GDAL 1.11 from here.
PROJ4 is a cartographic projections library. To download it, go to PROJ4 page or download PROJ 4.8.0 from here.
JSON-C is a library to read/write JSON objects in C. To downloat it, download JSON-C directly from here.
Now, our main dish. Download PostGIS from PostGIS page, or download it directly from here.
In the end of this section, you should have:
We will install all dependency in this order:
All the installation procedure will require root privilege.
Installation of PostgreSQL has been discussed in different article. See here to read it. You can also skip it if you have already installed PostgreSQL.
Next, install GEOS.
tar -jxf geos-3.4.2.tar.bz2
Next, install GDAL.
tar -Jxf gdal-1.11.0.tar.xz
Next, install PROJ4.
tar -zxf proj-4.8.0.tar.gz
When building PROJ4, you might encounter error like this:
jniproj.c:52:26: fatal error: org_proj4_PJ.h: No such file or directory
To solve it, go to src directory and edit jniproj.c then change this line
then resume the installation.
Next, install JSON-C. There is option to use the JSON-C from their github, but when I try it, it would break as JSON-C has removed some macro and functions. So let’s use it as is.
tar -zxf json-c-0.11.tar.gz
Now we are ready to install PostGIS.
Installing PostGIS is straightforward.
tar -zxf postgis-2.1.3.tar.gz
Installation finished. You should have PostGIS installed and ready.
PostGIS is an optional extension that must be enabled in each database you want to use it before you can use it. Installing the software is just the first step. And do not install it in the database called “postgres”.
Connect to database using psql. Run the following SQL:
-- Enable PostGIS (includes raster)
CREATE EXTENSION postgis;
-- Enable Topology
CREATE EXTENSION postgis_topology;
-- fuzzy matching needed for Tiger
CREATE EXTENSION fuzzystrmatch;
-- Enable US Tiger Geocoder
CREATE EXTENSION postgis_tiger_geocoder;
Example of Spatial SQL
-- Create table with spatial column
CREATE TABLE mytable (
id SERIAL PRIMARY KEY,
geom GEOMETRY(Point, 26910),
-- Add a spatial index
CREATE INDEX mytable_gix
USING GIST (geom);
-- Add a point
INSERT INTO mytable (geom) VALUES (
ST_GeomFromText('POINT(0 0)', 26910)
-- Query for nearby points
SELECT id, name
ST_GeomFromText('POINT(0 0)', 26910),