Install postgis 2.1 and QGIS on ubuntu 15.04

This is tested on a fresh installation of Ubuntu 15.04.


$ sudo apt-get install postgresql postgresql-contrib postgis postgresql-9.4-postgis-2.1 pgadmin3

# add a new user (need to be authenticated as a superuser)
$ sudo -u postgres createuser --superuser mysuperuser

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

Enter new password:mysuperuserpassword
Enter it again:

# create password for user postgre
$ sudo su
$ su - postgres

Now you can access the PostgreSQL prompt with the command:
$ psql

And then change the password for postgres role by typing :
$ \password postgres
Enter new password: postgres

Creating PostGIS templates:


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

# Load PostGIS SQL routines
$ psql -d template_postgis -f /usr/share/postgresql/9.4/contrib/postgis-2.1/postgis.sql -U mysuperuser -W -h localhost

$ psql -d template_postgis -f /usr/share/postgresql/9.4/contrib/postgis-2.1/spatial_ref_sys.sql -U mysuperuser -W -h localhost

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

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

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

$ psql -d template_postgis -c "VACUUM FREEZE;" -U mysuperuser -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 mysuperuser -W -h localhost

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

# in pgadmin you can create a new connection for localhost
name: localhost
host:localhost
port: 5432
maitenancedb: postgres
username:mysuperuser
password:mysuperuser

To open the shp2pgsql plugin just type


$ shp2pgsql-gui


To install an updated version of QGIS you better use their repositories, not the official ubuntu ones:



# First install grass
$ sudo apt-get install grass

# add QGIS respositories to our list of software providers by editing sources.list file
$ sudo gedit /etc/apt/sources.list

# Add this lines of the qgis repositories to your /etc/apt/sources.list (for ubuntu 15.04 you must choose vivid):
$ deb     http://qgis.org/debian vivid main
$ deb-src http://qgis.org/debian vivid main

# install
$ sudo apt-get update
$ sudo apt-get install qgis python-qgis qgis-plugin-grass

# in case of server key errors add this lines to the terminal and install again
$ gpg --keyserver keyserver.ubuntu.com --recv DD45F6C3
$ gpg --export --armor DD45F6C3 | sudo apt-key add -

To connect QGIS to a PostGIS database, you just need to click on the elephant icon (add PostGIS layers) and add a new connection like this:

qgis

Sources:

http://gishouse.blogspot.com.es/2013/01/how-to-install-postgresql-91-postgis-20.html

http://qgis.org/en/site/forusers/alldownloads.html#debian-ubuntu