PostGIS Installation

Jump to navigation Jump to search

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.

Installation Commands

Going off of

$ lsb_release --codename
Codename:       trusty
$ sudo sh -c 'echo "deb trusty-pgdg main" >> /etc/apt/sources.list'
$ wget --quiet -O - | sudo apt-key add -
$ sudo apt-get update
$ psql -V
psql (PostgreSQL) 9.5.4
$ sudo apt install postgresql-9.5-postgis-2.3
$ sudo apt-get install --no-install-recommends postgis

Note the last line. Without the --no-install-recommends line postgres assumes you're using the default configuration (in this case, postgis for postgresql 9.6). Since we're using a different flavor of postgis, we do not want this.

The postgis apt package is not the extension to postgres. It is a collection of command line utilities for importing and exporting data to a postgis db.

Creating a Postgis DB

Initial Setup

$ createdb --username researcher tigertest
$ psql --username researcher --dbname tigertest
> create extension postgis;
> select postgis_full_version(); -- sanity test and make sure installed and enabled
> \q

Bulk Download TIGER Shapefiles

For example, say we want all of the state-level place data. The first step is to find a programmatic URL that we can use. You can inspect the HTML on the HTML interface to place data to get the correct mapping of states/territories to two digit integer. Note that they are not necessarily sequential and there is not strictly fifty.

$ for i in $(seq -f "%02g" 1 80); do
    wget "${i}";
    sleep 3 # not necessary but a good guy scraper move
$ for f in *.zip; do unzip "$f"; done
$ for f in *.shp; do shp2pgsql -I "$f" | psql -U researcher -d tigertest; done
$ psql --username researcher --dbname tigertest
> select count(*) from tl_2016_01_place; -- 585

Also say we want to separate New York City, New York into a more granular by-borough set of polygons. Using City University of New York data, we will import this shapefile.

$ curl --insecure > /tmp/
$ cd /tmp
$ unzip
$ shp2pgsql -I nyu_2451_34505/nyu_2451_34505.shp | psql -U researcher -d tigertest
$ psql --username researcher --dbname tigertest
> select count(*) from nyu_2451_34505;
> \q