The following instructions describe how to install PostgreSQL and PostGIS on Linux and Windows, create the appropriate spatial databases, and tune database parameters for better performance. Please see the Mapnik page for more information on using Mapnik to render OpenStreetMap data.
In these examples, gisuser is used as the name of the user and gis is used as the name of the database
It is recommended to use PostgreSQL >= 9.3. Version 9.0 and older are end of life.
Install PostgreSQL and PostGIS
Ubuntu and Debian
sudo apt-get install postgresql postgresql-contrib postgis
OS X Leopard
Assuming you have darwinports,
% sudo port install postgresql83 % sudo port install postgis
or use the Mac OS X installers at KyngChaos.
|Fedora 16||Fedora 17 and above|
yum install postgis postgresql-server postgresql-setup initdb service postgresql start
yum install postgis postgresql-server postgresql-setup initdb systemctl start postgresql
To let the package manager know about PostGIS, you have to add a community package repository:
sudo zypper ar http://download.opensuse.org/repositories/Application:/Geo/openSUSE_13.1/ "Geo" sudo zypper refresh
Then you can install PostGIS:
sudo zypper install postgis postgresql-contrib
There are setup executables for PostGIS. Download the installer that matches your PostgreSQL version and run it. Your PostgreSQL server must be running. This will install some files in a subdirectory of your PostgreSQL installation and two database tables.
Installing osm2pgsql will pull postgres and postgis as dependencies:
Or you can install them explicitely with emerge postgresql-server postgis. The config file for pg_ctl will be in /etc/conf.d/postgresql-8.3 and for the server itself there'll be a sample file in /usr/share/postgresql-8.3/postgresql.conf.sample You can configure the cluster using emerge postgresql-server --config or manually if you want to customise it, then start it with /etc/init.d/posgresql-8.3 start before proceeding with the createuser instructions below.
On most systems you need to be authenticated as the PostgreSQL super user (usually named postgres) in order to execute many of the commands below.
sudo -u postgres createuser gisuser sudo -u postgres createdb --encoding=UTF8 --owner=gisuser gis
These commands can be issued from the Admin tool GUI or run by navigating to the bin directory (e.g. C:\Program Files\PostgreSQL\8.3\bin) in an command prompt run as Administrator, then running:
C:\Program Files\PostgreSQL\8.3\bin\createuser -U postgres gisuser C:\Program Files\PostgreSQL\8.3\bin\createdb -U postgres -E UTF8 -O gisuser gis C:\Program Files\PostgreSQL\8.3\bin\createlang -U postgres plpgsql gis
If needed, the commands 'dropdb gis' and 'dropuser gisuser' can be used. In particular, you might find it necessary to make the user a postgres administrator (you will be asked for this when creating the user), to simplify user permissions.
You have to create a user in Postgresql that will be used to create all tables and data. Then some SQL scripts containing vital mapping-related instructions need to be executed. These instructions generate the user gisuser and the password "something". To change that, replace all occurences of "gisuser" and "something", respectively.
sudo su -c "createuser --superuser gisuser" postgres sudo su -c "createdb -E UTF8 -O gisuser gis" postgres sudo su -c "createlang plpgsql gis" postgres echo "ALTER USER gisuser WITH PASSWORD 'something';" | psql -d gis
Please note: using the default openSUSE 11.2 configuration of PostgreSQL, you will definitely run in to this issue: Mapnik#Authentication failed. Follow the steps listed there to fix it.
You need to activate PostGIS on the newly created database.
psql --username=postgres --dbname=gis -c "CREATE EXTENSION postgis;" psql --username=postgres --dbname=gis -c "CREATE EXTENSION postgis_topology;"
Populate the database
The database can be populated by :
could not access file "$libdir/postgis-1.5": No such file or directory
This error shows up when both postgresql-9.0 and postgresql-8.4 are installed (Debian/testing). Uninstalling 9.0 helps, it should be also possible to switch the active toolset version.
createdb: database creation failed: ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)
http://journal.tianhao.info/2010/12/postgresql-change-default-encoding-of-new-databases-to-utf-8-optional/ Change default template to UTF-8]] (Note: I used UTF8 instead UNICODE as that page says.) (I got this error on Postgis 9.0.7, Mandriva 2011)
Enable easy database login by user gisuser you must change some lines in one of the database configuration files.
# for Ubuntu <=9.10: sudo gedit /etc/postgresql/8.3/main/pg_hba.conf # for Ubuntu >=10.04: sudo gedit /etc/postgresql/8.4/main/pg_hba.conf # for Ubuntu >=12.04: sudo gedit /etc/postgresql/9.1/main/pg_hba.conf # for Ubuntu >=14.04: sudo gedit /etc/postgresql/9.3/main/pg_hba.conf
Near to the bottom of the file you will find these lines:
local all all ident host all all 127.0.0.1/32 md5
On some setups you may see an entry for ipv6:
host all all ::1/128 md5
Change the words ident and md5 each to trust and close the editor. Now reload the database configuration, e.g.
sudo -u postgres /usr/lib/postgresql/9.1/bin/pg_ctl reload
- PostGIS in Action book by Regina O. Obe and Leo S. Hsu, Manning Publications (2015)
- Installation chapter of the PostGIS 2.0 Manual