PostGIS is a spatial database extension for PostgreSQL RDBMS. It provides geometric data types and a number of geometric functions to query geometric data. PostGIS needs to be installed separately after installing PostgreSQL Server. In this tutorial, we are going to have a look at how to install PostGIS 2.1 for the latest PostgreSQL 9.3 in Linux/ Ubuntu. Obviously, prior to installing PostGIS we need to install PostgreSQL.
If you didn’t have PostgreSQL 9.3 installed, you may go through the following tutorial first:
How to: Install PostgreSQL 9.3 on Ubuntu – Complete Guide
So, you have the latest PostgreSQL 9.3 on your Ubuntu, let’s install PostGIS now:
- Remove old PostGIS Installation
The first step is to remove older version of PostGIS if any. For this, open a Terminal and run:sudo apt-get purge postgis
- Add and set up repository:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" >> /etc/apt/sources.list.d/postgresql.list'
- Install PostGIS 2.1 using aptitude:
sudo apt-get update sudo apt-get install postgresql-9.3-postgis-2.1 -f
Now, let’s see how we can create a PostGIS enabled data base. We have two ways of doing this, in which the first one is the latest and simplehttp://technobytz.com/wp-admin/profile.php one:
-
- Using the CREATE EXTENSION statement
This is as simple as running a query in the data base where you want to enable PostGIS:CREATE EXTENSION postgis;
OR
- Create database template for PostGIS
This is an old method of doing the same. Creating a template for PostGIS will make it easy to enable PostGIS for every new database you create:createdb -E UTF8 template_postgis2.1 psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis2.1"
Now, we have to run an SQL script comes along with PostGIS in the template:
psql -d template_postgis2.1 -f /usr/share/postgresql/9.3/extension/postgis--2.1.1.sql psql -d template_postgis2.1 -c "GRANT ALL ON geometry_columns TO PUBLIC;" psql -d template_postgis2.1 -c "GRANT ALL ON geography_columns TO PUBLIC;" psql -d template_postgis2.1 -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"
Create a test database
Let’s test the PostGIS installation by creating a test database:createdb test_db -T template_postgis2.1
In test_db you can run the following statement to make sure that you installed and configured PostGIS correctly:
test_db=# select postgis_version(); postgis_version--------------------------------------- 2.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1 (1 row)
- Using the CREATE EXTENSION statement
- Enable PostGIS for an existing Database
If you don’t want to use the CREATE EXTENSION statement and want to enable PostGIS for an already existing database. It is simple enough, you just need to run the PostGIS 2.1 script in your database:psql -d your_db_name -f /usr/share/postgresql/9.3/extension/postgis--2.1.1.sql
That’s it. You have installed PostGIS 2.1. Now, you can start loading geometric data and perform various operations on it!
7 comments
Great guide! Saved me a lot of time! Thank you!
One note. The version is 2.1.2 now. So the setup script should be:
/usr/share/postgresql/9.3/extension/postgis–2.1.2.sql
Yes. It depends on the version of the PostGIS you have installed.
[…] How to: Install PostGIS 2 for PostgreSQL 9.3 on Ubuntu [Guide] […]
Cool! Thank you for the update 😉
[…] recently reinstalled postgis on my pc, following these instructions link. The installation is successful, and for several days work using PostgreSQL and PostGIS without […]
[…] How to: Install PostGIS 2 for PostgreSQL 9.3 on Ubuntu [Guide] […]
Please add following command to your step by step guide as it will guide user to add proper apt url as per their OS
Run the following command and note down the output. It is the codename of your Ubuntu release.
lsb_release -c
Replace the codename in the below command with the one you got from above command and run:
sudo sh -c ‘echo “deb http://apt.postgresql.org/pub/repos/apt/ codename-pgdg main” >> /etc/apt/sources.list.d/postgresql.list