PostGIS (pronounced post-jis) is a spatial database extender for PostgreSQL that adds support for geometry types and geospatial functions such as point, line, and polygons.

Install PostgreSQL

Before we can install PostGIS we must first install PostgreSQL. We'll use brew to install PostgreSQL, open Terminal and run the following command:

brew install postgres

You should see output like:

Install PostGIS

We'll also use brew to install PostGIS, open Terminal and run the following command:

brew install postgis

You should see output like:

Start PostgreSQL

Before we start PostgreSQL let's set the PGDATA environment variable, add the following line to your ~/.bash_profile (or if you don't want to maintain two separate config files for login and non-login shells, put your common settings in ~/.bashrc and make sure you source it from your ~/.bash_profile):

 export PGDATA=/usr/local/var/postgres

Load the changes into your current shell:

source ~/.bash_profile

Now, we can start PostgreSQL by using the pg_ctl command line utility:

pg_ctl start 

Let’s check if PostgreSQL is running:

pg_ctl status

You should see output like:

Create a Database

Brew initialises the PostgreSQL cluster during installation so we can jump right in and create a new database:

createdb postgis_test

Now, we can connect to the new database by using the psql command line utility:

psql postgis_test 

You should see output like:

To enable PostGIS, run the following command:

CREATE EXTENSION postgis;

You should see output like:

The best way to learn PostGIS is to try it, start by checking the version:

SELECT postgis_full_version();

You should see output that includes the version of PostGIS, as well as the versions of the supporting GEOS, PROJ, GDAL, LIBXML and LIBJSON libraries:

POSTGIS="2.1.7 r13414"
GEOS="3.4.2-CAPI-1.8.2 r3921" 
PROJ="Rel. 4.9.1, 04 March 2015" 
GDAL="GDAL 1.11.2, released 2015/02/10" 
LIBXML="2.9.2" 
LIBJSON="UNKNOWN" RASTER

Then, take a look at the PostgreSQL & PostGIS Cheat Sheet on GitHub.

To quit psql, type the following command:

\q

When you have finished, don't forget to stop PostgreSQL:

pg_ctl stop
References: