Wednesday, January 23, 2013

How to install PostgreSQL 9.1, PostGIS 2.0, pgAdmin 3 and QGIS on Ubuntu 12.10


Here is a simple tutorial on how to install PostgreSQL 9.1, PostGIS 2.0, pgAdmin 3 and QGIS on Ubuntu 12.10.
Open terminal and execute the following code:
Note: you only need to type (or copy/paste) text that comes after $ sign. Text that comes after # is a comment and it will not be executed. Internet connection is required. 

#Install PostgreSQL 9.1
$ sudo apt-get install postgresql-9.1

#Required for PostGIS: Add the ppa (Personal Package Archive) to the current repository
$ sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable

#Required for PostGIS: Update the local package index
$ sudo apt-get update

#Install PostGIS
$ sudo apt-get install postgis

#Install pgAdmin 3
$ sudo apt-get install pgadmin3

#Install Quantum GIS (QGIS)
$ sudo apt-get install qgis

OK, now we should have PostgreSQL with PostGIS up and running. Next thing we need to do is to add new SuperUser. To do that, open terminal and execute the following code:

#Create user zoranp
$ sudo -u postgres createuser
Enter name of role to add: zoranp
Shall the new role be a superuser? (y/n) y

#Set password for created user
$ sudo -u postgres psql postgres
postgres=# \password zoranp

Enter new password:
Enter it again:

Note: You can replace zoranp username with your own.

Next step is to create PostGIS template. Execute the following code in terminal:

#Create the template spatial database
$ createdb -E UTF8 -T template0 template_postgis -U zoranp -W -h localhost

#Load PostGIS SQL routines
$ psql -d template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-2.0/postgis.sql -U zoranp -W -h localhost

$ psql -d template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-2.0/spatial_ref_sys.sql -U zoranp -W -h localhost

#Enable users to alter spatial tables
$ psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;" -U zoranp -W -h localhost

$ psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;" -U zoranp -W -h localhost

#Garbage-collect and freeze
$ psql -d template_postgis -c "VACUUM FULL;" -U zoranp -W -h localhost

$ psql -d template_postgis -c "VACUUM FREEZE;" -U zoranp -W -h localhost

#Allow non-superusers the ability to create from this template
$ psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';" -U zoranp -W -h localhost

$ psql -d postgres -c "UPDATE pg_database SET datallowconn='false' WHERE datname='template_postgis';" -U zoranp -W -h localhost

Note: You should check if postgis.sql and spatial_ref_sys.sql files are in /usr/share/postgresql/9.1/contrib/postgis-2.0/ directory. If they are not, you need to find correct directory and replace path in the code.



Screencast:

No comments:

Post a Comment