How to install PostGIS 2.0 on Ubuntu 12.04 LTS (precise) from source

Adapted from: http://trac.osgeo.org/postgis/wiki/UsersWikiPostGIS20Ubuntu1204src

Spatially enabling a database

With PostgreSQL 9.1, there are two methods to add PostGIS functionality to a database: 
- using extensions, 
- using enabler scripts.

PostGIS Extension for PostgreSQL

Spatially enabling a database using extensions is a new feature of PostgreSQL 9.1.

Connect to your database using pgAdmin or psql, and run the following commands. 
To add postgis with raster support:

CREATE EXTENSION postgis;

To add topology support, a second extension can be created on the database:

CREATE EXTENSION postgis_topology;

Notes

- No PostGIS topology functionalities are yet available from GeoDjango, so the creation of the postgis_topology extension is entirely optional
- Raster is required for the extension installation method for PostgreSQL.

Install from sources on Ubuntu

Here we avoid postgis_topology installation; to have topology installed, follow:
http://trac.osgeo.org/postgis/wiki/UsersWikiPostGIS20Ubuntu1204src

$ sudo apt-get install libxml2-dev libproj-dev
$ sudo apt-get install libgdal1-dev
$ wget http://download.osgeo.org/postgis/source/postgis-2.0.4.tar.gz
$ tar xfz postgis-2.0.4.tar.gz
$ cd postgis-2.0.4
$ ./configure --without-topology
$ make
$ sudo make install
$ sudo ldconfig
$ sudo make comments-install

Note per l’esecuzione di unit tests in Django


- Django unit tests runner uses CREATE EXTENSION to build test database
  when settings specify 'ENGINE'= django.contrib.gis.db.backends.postgis'

- L'utente del db deve essere superuser (per postgres) al fine di
  evitare l'errore:

DatabaseError: permission denied to create extension "postgis"
HINT:  Must be superuser to create this extension.

quindi:

psql# alter role  superuser;

Fix per Postgresql 9.3

# .../postgis-2.0.x/postgis/geometry_estimate.c

add:
//add
#if POSTGIS_PGSQL_VERSION >= 93
  #include "access/htup_details.h"
#endif

then repeat make, make install

See: http://stackoverflow.com/questions/16644163/solved-postgis-2-0-so-undefined-symbol-getstruct

Postgresql

Setup on Mac with MacPorts

$ sudo port install postgresql91 postgresql91-server

To create a database instance, after install do
 sudo mkdir -p /opt/local/var/db/postgresql91/defaultdb
 sudo chown postgres:postgres /opt/local/var/db/postgresql91/defaultdb
 # sudo su postgres -c '/opt/local/lib/postgresql91/bin/initdb -D /opt/local/var/db/postgresql91/defaultdb' 
 sudo su postgres -c '/opt/local/lib/postgresql91/bin/initdb -D /opt/local/var/db/postgresql91/defaultdb --lc-collate="en_US.UTF-8" --lc-ctype="en_US.UTF-8"' 

To tweak your DBMS, consider increasing kern.sysv.shmmax by adding an increased kern.sysv.shmmax .. to /etc/sysctl.conf

Now you can start the database server using:

$ sudo su postgres -c '/opt/local/lib/postgresql91/bin/pg_ctl -D /opt/local/var/db/postgresql91/defaultdb -l logfile start'

To start the service automatically:
$ sudo launchctl load -w -F /Library/LaunchDaemons/org.macports.postgresql84-server.plist

Reload

$ sudo /etc/init.d/postgresql {start|stop|restart|reload|force-reload|status}

Reload (on Mac)

$ sudo launchctl unload /Library/LaunchDaemons/org.macports.postgresql84-server.plist
$ sudo launchctl load /Library/LaunchDaemons/org.macports.postgresql84-server.plist

Vedere anche : http://www.jonathandean.com/2011/08/postgresql-8-4-on-mac-os-x-10-7-lion/

Oppure:

    1) Using the superuser account you can query the location of the data directory through SQL:
    # select name, setting from pg_settings where name = 'data_directory';

    2) With that information you can supply the data directory to the pg_ctl command using the -D switch:
    $ sudo su
    # su postgres
    $ pg_ctl -D PATH restart

    Thanks to: http://stackoverflow.com/questions/7990539/restarting-postgres-on-mac