Digital Geography

14. September 2013

OpenSource QGIS + PostGIS installation: “the Ubuntu way”

A lot of you out there are probably working on Windows Systems using ArcGIS and I have to admit that the possibilities with ArcINFO in the background and a proper GDB (GeoDataBase) are really big. But let’s be honest: A lot of your tasks are more or less everyday business, right?! So let us have a look on building a GIS workplace using QGIS 1.8 (which will be updated the big way in some days/weeks) and PostGIS which is a spatial enhancement of PostgreSQL.

Prerequisties

I am starting with a brand new build of Ubuntu 12.04.3 LTS in the 64bit flavour. You may download it using the Ubuntu pages. As you may have seen some other tutorials on Ubuntu we will start with some basic tasks and getting the catalog up2date and install qgis which will consume data from the PostGIS database. Therefore we will used the stable PPA from ubuntugis. Keep in mind that packages there are a little older but they are working great!!!

sudo add-apt-repository ppa:ubuntugis/ppa
sudo apt-get update
sudo apt-get install qgis
sudo apt-get install python-psycopg2
sudo chown -R "your username" ~/.qgis

You may have noticed the last two lines. Those provide the possibility to install some extensions we will need to work with rasters in QGIS/PostGIS.
Now we are ready to look into the PostgreSQL corner. We need to install this first and will concentrate later on the PostGIS extension:

sudo apt-get install build-essential postgresql-9.1 postgresql-server-dev-9.1 libxml2-dev proj libjson0-dev xsltproc docbook-xsl docbook-mathml gettext postgresql-contrib-9.1

Additionally we will install some new GDAL libraries in the current version 1.10.0 to support our work on raster data.

sudo apt-get install libgdal-dev

PostGIS installation

With the stable ppa the sudo apt-get install postgresql-9.1-postgis option succeeds [/sourcecode]
To make some basic scripts work directly you should add the following lines in your terminal:

sudo ln -sf /usr/share/postgresql-common/pg_wrapper /usr/local/bin/shp2pgsql
sudo ln -sf /usr/share/postgresql-common/pg_wrapper /usr/local/bin/pgsql2shp
sudo ln -sf /usr/share/postgresql-common/pg_wrapper /usr/local/bin/raster2pgsql

PostGIS configuaration

Now lets make it simple. This doesn’t mean it’s safe as we are using the standard user of postgres with all his rights.
First of all we will set a custom password for the user postgres in PostgreSQL which is the standard user off PostgreSQL and also on your system. Yet those are different instances (keep this in mind):

sudo -s -u postgres psql

We will now work in the “psql” program. You can see this by the Number sign (‘#’) at the end of the prompt. You can set the password using

\password postgres

Furtermore we will add the postgis extension to the postgres database so it is spatially enabled as well:

CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;

You can create now a new database using this database postgres as a template (maybe you should remove the adminpack using pgAdmin III 😉 )
There is also another way to create a spatial database using some lines of code. So let’s finish our journey by creating a database gis_db and add spatial support by using some lines for postgis and additional support like raster and topologies:

sudo -u postgres createdb gis_db
sudo -u postgres psql -d gis_db -f /usr/share/postgresql/9.1/contrib/postgis-2.0/postgis.sql
sudo -u postgres psql -d gis_db -f /usr/share/postgresql/9.1/contrib/postgis-2.0/spatial_ref_sys.sql
sudo -u postgres psql -d gis_db -f /usr/share/postgresql/9.1/contrib/postgis-2.0/postgis_comments.sql
sudo -u postgres psql -d gis_db -f /usr/share/postgresql/9.1/contrib/postgis-2.0/rtpostgis.sql
sudo -u postgres psql -d gis_db -f /usr/share/postgresql/9.1/contrib/postgis-2.0/raster_comments.sql
sudo -u postgres psql -d gis_db -f /usr/share/postgresql/9.1/contrib/postgis-2.0/topology.sql
sudo -u postgres psql -d gis_db -f /usr/share/postgresql/9.1/contrib/postgis-2.0/topology_comments.sql

QGIS or check it out

Now fire up QGIS and enable the SPIT plugin:

SPIT plugin: Plugins -> Manage Plugins


Now we need to connect to our new geodatabase by filling in all we got:

Connecting to PostGIS using SPIT@QGIS

If you need some data for adding: Try a shapefile for import with SPIT. If you want to save some raster data in your PostGIS database or would like to see some examples of importing data using SPIT and the database manager in QGIS you should have a look into the Windows-version of this article: PostGIS/QGIS on Windows

Enjoy!!!

  • Good tip for users of linux.
    I will go use this post in my classes.
    tks

    • hi sadeckgeo. please test for yourself first. I’ve seen some problems in importing raster data… that was working fine in the windows version

    • Hi Sadeckgeo. I’ve updated the article to work with stable packages and also raster import works now totally fine…

  • Luigi Pirelli

    beware that SPIT plugin will be removed in favor of #processing core functions

    follow discussion in qgis-developer mailing list

    • Thanks for the hint. I’ll probably make an update for QGiS 2 though.