4. Prepare Data


To be able to use pgRouting, data has to be imported into a database.

4.1. Prepare the database

pgRouting is installed as extension. This requires:

  • PostgreSQL 9.1 or higher
  • PostGIS 2.x installed as extension

These requirements are met on OSGeoLive, then open a terminal window ctrl-alt-t and follow the instructions.


If OSGeo Live is not being used.

OSGeo Live’s account name on the database is "user". To easily use the workshop when not using OSGeo Live this extra steps are needed:

# work on the home folder

# login to postgres
psql -U postgres

-- Create "user"

-- exit psql

# Add the user to .pgpass
echo :5432:*:user:user >> .pgpass

4.1.1. Create a pgRouting compatible database.


Depending on the postgres configureation -U <user> is needed on psql commands

# Create the database
createdb city_routing

# login as user "user"
psql city_routing

-- add PostGIS functions

-- add pgRouting functions

-- Inspect the pgRouting installation
\dx+ pgrouting

-- View pgRouting version
SELECT pgr_version();

-- exit psql

4.2. Get the Workshop Data

The pgRouting workshop will make use of OpenStreetMap data, which is already available on OSGeo Live. This workshop will use the Dar Es Salaam city data and is a snapshot of August-2018.

4.2.1. Make a directory for pgRouting data manipulation

mkdir ~/Desktop/workshop
cd ~/Desktop/workshop

4.2.2. Getting the data Option 1) When using OSGeo Live

OSGeo Live comes with osm data from the city of Dar Es Salaam.

bzcat ~/data/osm/$CITY.osm.bz2 > $CITY.osm Option 2) Download data form OSGeo Live website

The exact same data can be found on the OSGeo Live download page.

wget -N --progress=dot:mega \
bunzip2 $CITY.osm.bz2 Option 3) Download using Overpass XAPI.

The following downloads the latest OSM data on using the same area. Using this data in the workshop can generate variations on the results, due to changes since Jun-2017.

wget --progress=dot:mega -O "$CITY.osm" "http://www.overpass-api.de/api/xapi?*[bbox=${BBOX}][@meta]"

More information how to download OpenStreetMap information can be found in http://wiki.openstreetmap.org/wiki/Downloading_data

An alternative for very large areas is to use the download services of Geofabrik.

4.3. Upload Data to the database

The next step is to run osm2pgrouting converter, which is a command line tool that inserts your data in the database, “ready” to be used with pgRouting. Additional information about osm2pgrouting can be found at the Appendix: osm2pgrouting Import Tool

For this step:

  • the osm2pgrouting default mapconfig.xml configuration file is used
  • and the ~/Desktop/workshop/DS_TS.osm data.
  • with the city_routing database

From a terminal window ctrl-alt-t.

4.3.1. Run the osm2pgrouting converter

cd ~/Desktop/workshop
osm2pgrouting \
    -f DS_TZ.osm \
    -d city_routing \
    -U user


Depending on the osm2pgrouting version -W password is needed


Execution starts at: Tue Sep 25 02:15:40 2018

           COMMAND LINE CONFIGURATION             *
Filename = DS_TZ.osm
Configuration file = /usr/share/osm2pgrouting/mapconfig.xml
host = localhost
port = 5432
dbname = city_routing
username = user
password =
prefix =
suffix =
Don't drop tables
Don't create indexes
Don't add OSM nodes
Testing database connection: city_routing
database connection successful: city_routing
Connecting to the database
connection success

Creating tables...
TABLE: ways_vertices_pgr created ... OK.
TABLE: ways created ... OK.
TABLE: pointsofinterest created ... OK.
TABLE: configuration created ... OK.
Opening configuration file: /usr/share/osm2pgrouting/mapconfig.xml
    Parsing configuration

Exporting configuration ...
  - Done
Counting lines ...
  - Done
Opening data file: DS_TZ.osm  total lines: 724406
    Parsing data

End Of file

    Finish Parsing data

Adding auxiliary tables to database...

Export Ways ...
    Processing 33232 ways:
[******************************|                    ] (60%) Total processed: 20000   Vertices inserted: 2774  Split ways inserted 3284
[**************************************************|] (100%) Total processed: 33232  Vertices inserted: 1188  Split ways inserted 2366

Creating indexes ...

Processing Points of Interest ...
size of streets: 33232
Execution started at: Tue Sep 25 02:15:40 2018
Execution ended at:   Tue Sep 25 02:15:44 2018
Elapsed time: 4.06 Seconds.
User CPU time: -> 2.41657 seconds

Tables on the database

psql -d city_routing -c "\d"

If everything went well the result should look like this:

               List of relations
Schema |           Name           |   Type   | Owner
public | configuration            | table    | user
public | configuration_id_seq     | sequence | user
public | geography_columns        | view     | user
public | geometry_columns         | view     | user
public | pointsofinterest         | table    | user
public | pointsofinterest_pid_seq | sequence | user
public | raster_columns           | view     | user
public | raster_overviews         | view     | user
public | spatial_ref_sys          | table    | user
public | ways                     | table    | user
public | ways_gid_seq             | sequence | user
public | ways_vertices_pgr        | table    | user
public | ways_vertices_pgr_id_seq | sequence | user
(13 rows)