Ubuntu GIS from Scratch: Step 3 – PostGIS

Welcome to Step Three in our exploration of open source GIS using Ubuntu. In order to grasp the full power of GIS, we need to spatially extend our database (PostgreSQL, which was installed in the first step). The spatial extension for PostgreSQL is PostGIS. In the second step, we installed GEOS and GDAL, two major dependencies of PostGIS. Now we’re ready for the real thing. Once again, the latest version of PostGIS is not available from the Ubuntu Software Center, so we have to install from source.

Warning: If you haven’t completed the previous steps, installing and configuring PostgreSQL, GEOS, and GDAL, none of the following will work at all – you’ll get all kinds of crazy errors. So unless you have all three installed and working on your machine already, you’ll need to go back and do that before proceeding.

1. The first thing to do is download the PostGIS tarball, which can be found here:
The latest version as of this writing is postgis-2.0.1.tar.gz
You may notice that the PostGIS downloads site refers to this release as a “stable” release. A stable release is a version of the software that the development team has decided is bug-free and ready for general use. When a release becomes stable, development is frozen – any further development becomes part of the next release. This is in contrast to an “unstable” or “testing” release, which is undergoing current development, and may be changing on a daily basis. Unless you have a specific reason to do otherwise, always choose a stable release when downloading a source package.

2. The source package is a gzipped tarball, so switch to the Downloads directory and issue the appropriate tar command.
cd ~/Downloads
tar zxf postgis-2.0.1.tar.gz
cd postgis-2.0.1
In the previous post, we used two configure apps, geos-config and gdal-config, to get information about the GEOS and GDAL installations. Now we’re going to feed the output of those apps into the configure statement of PostGIS. This is not usually required, but it makes the installation go a lot easier. The apps were installed in /usr/local/bin.
./configure –with-geosconfig=/usr/local/bin/geos-config –with-gdalconfig=/usr/local/bin/gdal-config –with-topology –with-raster
We’re also telling configure that we are definitely interested in having topology support and raster support available in the database. When configure is done, it is time to build and install the software:
sudo make install

3. As with our previous source installations, it is a good idea to update the linker database after installing new libraries:
sudo ldconfig
At this time, let’s restart the PostgreSQL database, so that PostGIS will be running:
sudo service postgresql restart

4. During the PostGIS installation, a database template called template_postgis was created. However, it isn’t ready to use yet, because it doesn’t have the spatial extensions enabled. So let’s go ahead and do that. First is the main PostGIS extension:
psql template_postgis -c “create extension postgis;”
After running this command, you will see the message:
And now to enable the topology extension:
psql template_postgis -c “create extension postgis_topology;”
Although the newest version of PostGIS is implemented through extensions, it would also be nice to have backwards compatibility with databases managed by previous versions of PostGIS. So we’ll install the legacy functions too:
psql template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-2.0/legacy.sql
This command will run quickly and generate a couple pages of output, most of which will look like:

5. The public schema in template_postgis now has 4 table views, called geography_columns, geometry_columns, raster_columns, and raster_overviews. There is also a table called spatial_ref_sys where spatial reference system definitions are stored. We want all five of these to be accessible by any PostGIS users, which requires 5 different commands. Each command will be confirmed by “GRANT”:
psql -d template_postgis -c “GRANT ALL ON geometry_columns TO PUBLIC;”
psql -d template_postgis -c “GRANT ALL ON geography_columns TO PUBLIC;”
psql -d template_postgis -c “GRANT ALL ON raster_columns TO PUBLIC;”
psql -d template_postgis -c “GRANT ALL ON raster_overviews TO PUBLIC;”
psql -d template_postgis -c “GRANT ALL ON spatial_ref_sys TO PUBLIC;”
Now, that the permissions and extensions of the template are set up just the way we like them, we want to make it difficult for anyone to tamper with it. This is accomplished by not allowing users to connect to the template directly:
psql -d postgres -c “UPDATE pg_database SET datallowconn=’false’ WHERE datname=’template_postgis’;”
(later, if you want to modify the template, you must run this command again, but setting datallowconn to ‘true’ instead of ‘false’)
One last configuration command keeps anyone from accidentally erasing the template:
psql -d postgres -c “UPDATE pg_database SET datistemplate=’true’ WHERE datname=’template_postgis’;”
(later, if you want to erase the template, you can run this command again, but setting datistemplate to ‘false’ instead of ‘true’)
Finally, restart the PostgreSQL server again to reflect our changes:
sudo service postgresql restart

6. At this point, we can look over our work in pgAdmin. Upon opening and connecting, you will see that there is a new database called template_postgis that you cannot connect to. However, it is quite simple to create a new instance of the template to explore. Go to Edit > New Object > New Database; or right click the template_postgis icon to select New Object > New Database. In the New Database dialog, on the Properties tab, give your new database a name and select an owner from the dropdown box. Now, on the Definition tab, use the Template dropdown box to select template_postgis, and then click OK. Your new database will appear in the left column, and you can use the [+] icons to expand and browse the database tree. You will see two schemas, one called public, and one called topology. A schema is the top level of structure in the database, which you can use to organize tables logically or conceptually. All of the main PostGIS functions and definitions are stored in the public schema, while the topology-related functions are in the topology schema. Later, when we upload data to the database, we’ll create a new schema, to keep our data and functions separate, which makes them easier to move and delete.

At this point, you have a fully functional spatially-enabled database system running on your computer. Coming up in the next installment, we’ll be uploading a shapefile into the database and exploring it. Later, we will install QGIS and load data from a PostGIS database directly into a graphical GIS platform.


Thomas on November 15, 2012 at 15:20.


it is the first tutorial for installing postgis. Thank you. But i run into a problem. I installed postgis, no problem. But i’m not able to enable the extension postgis.

here is the output of the terminal:

thomas@thomas-thinkpad-t530:~/Downloads/postgis-2.0.1$ psql template_postgis -c “create extension postgis”
psql: warning: extra command-line argument “postgis”” ignored
psql: FATAL: Peer authentication failed for user “extension”

what is the problem?

Thank you in advance for your help


leigh on November 15, 2012 at 16:26.

All apologies, there is a typo in that command. All SQL commands, including those passed via psql using the -c flag, need to be terminated with a semicolon.

psql template_postgis -c “create extension postgis;”
psql template_postgis -c “create extension postgis_topology;”

The instructions have been modified to correct this error. Thanks for pointing it out!