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
JSON-C is a library to read/write JSON objects in C. To downloat it, download JSON-C 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 cd geos-3.4.2 ./configure make -j4 make install
Next, install GDAL.
tar -Jxf gdal-1.11.0.tar.xz cd gdal-1.11.0 ./configure make -j4 make install
Next, install PROJ4.
tar -zxf proj-4.8.0.tar.gz cd proj-4.8.0 ./configure make -j4 make install
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 cd json-c-0.11 ./configure make -j4 make install
Now we are ready to install PostGIS.
Installing PostGIS is straightforward.
tar -zxf postgis-2.1.3.tar.gz cd postgis-2.1.3 ./configure make -j4 make install
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), name VARCHAR(128) ); -- Add a spatial index CREATE INDEX mytable_gix ON mytable USING GIST (geom); -- Add a point INSERT INTO mytable (geom) VALUES ( ST_GeomFromText('POINT(0 0)', 26910) ); -- Query for nearby points SELECT id, name FROM mytable WHERE ST_DWithin( geom, ST_GeomFromText('POINT(0 0)', 26910), 1000 );GIS, linux, slackware