PostGIS/Installation

From OpenStreetMap Wiki
Jump to navigation Jump to search

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

The main OpenStreetMap Mapnik styles make use of PostgreSQL and PostGIS as part of the rendering pipeline. Creating a database is the same or similar for osmosis and osm2pgsql.

It is recommended to use PostgreSQL >= 9.3. Version 9.0 and older are end of life.

Install PostgreSQL and PostGIS

Ubuntu and Debian

You should first setup Postgresql official repository as described here.

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib postgis postgresql-9.6-postgis-2.3 postgresql-9.6-postgis-2.3-scripts

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

sudo dnf install postgis postgresql-server postgresql
sudo postgresql-setup --initdb
sudo systemctl start postgresql

After installing PostgreSQL see /usr/share/doc/postgresql/README.rpm-dist for more information.

openSUSE

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_Leap_15.1/ "Application:Geo"
sudo zypper refresh

If you are not on openSUSE_Leap_15.1, check http://download.opensuse.org/repositories/Application:/Geo/ for supported distributions and replace "openSUSE_Leap_15.1" by your distribution.

Then you can install PostGIS:

sudo zypper search postgis
sudo zypper install postgresql96-postgis postgresql96-contrib

See the output of the first command for the supported versions.

For more detailed help on Postgress, see https://en.opensuse.org/SDB:PostgreSQL

Windows

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.

Gentoo

Installing osm2pgsql will pull postgres and postgis as dependencies:

emerge osm2pgsql

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.

Create database

General

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
Windows

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.


openSUSE 13.1

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.

Activate PostGIS

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 :

Troubleshooting

Problem:

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.


Problem:

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

References


See also