Skip to content

Installing the Backend (Database)

Grant edited this page Aug 21, 2023 · 5 revisions

When setting up the database, you have two options: basic and advanced. For a basic setup, you'll have access to every feature of OSM Finder except shape comparisons. A basic setup will also take significantly less time to process. For the advanced option, you'll be able to search the database for shapes, but be prepared to wait several hours while the database is set up. Both options require the same first few steps, but step 4 will be different depending on which option you choose.

1. Ensure you have PostgreSQL and PostGIS installed, and then install osm2pgsql version 1.7.1 or higher.

2. Optional: tune your PostgreSQL server.

3. Download OpenStreetMap data for your area of interest.

4. Download flex.lua and run the following commands (in Linux):

    sudo -u postgres createuser osmuser 
    sudo -u postgres createdb --encoding=UTF8 --owner=osmuser osm 
    sudo -u postgres psql osm --command='CREATE EXTENSION postgis;' 
    sudo -u postgres psql osm --command='CREATE EXTENSION hstore;' 

Then, follow the instructions below for either the basic or advanced setup.

4.1. Option 1: Basic Setup (without shapes)

For the basic installation, simply run this command:

osm2pgsql -d osm -U osmuser -O flex -S flex.lua washington-latest.osm.pbf
  • Note: you may need to move files around and change directory/file permissions appropriately to ensure that the postgres user can access washington-latest.osm.pbf and flex.lua.
  • Note: if you get an error saying "peer authentication failed" when you run the last command, check out the solution at https://stackoverflow.com/a/26735105/1941353, but replace user 'postgres' with 'osmuser'.

4.2. Option 2: Advanced Setup (with shapes)

4.2.1. For the advanced installation, you'll need a C compiler, such as GCC. If you built osm2pgsql from source, you'll likely already have gcc installed. If you didn't, you'll have to figure out which C compiler works best for your system and install it. Once installed, run the following command in the main directory of the repository:

gcc -shared -fPIC -I/usr/include/lua5.3 -llua5.3 -o shapeComparison.so shapeComparison.c

This command compiles and links shapeComparison.c so that it can be used by osm2pgsql. Depending on your operating system and version of Lua installed, you may need to change the -I/usr/include/lua5.3 -llua5.3 part of the command. The /usr/include/lua5.3 file path is simply the directory that contains lua.h and similar Lua v5.3 files. Once you find the same directory on your system, don't forget to prepend the file path with -I. The -llua5.3 part of the command may change to correspond with the Lua version on your system (e.g., -llua5.4), or it may just be -llua, but it will always be prepended with -l.

4.2.2. Once you compile shapeComparison.c without errors, find and open flex.lua in a text editor. At the top, you should see shape_comparison = 0, which disables shapes comparisons by default. Go ahead and change the value to 1 or another option to enable the feature.

4.2.3. Finally, run the following command:

osm2pgsql -d osm -U osmuser -x -O flex -S flex.lua washington-latest.osm.pbf
  • Notice the additional -x argument that's not present in the basic setup option. This ensures that osm2pgsql doesn't skip any node, even nodes without attributes, which is needed to properly compare shapes.
  • Note: you may need to move files around and change directory/file permissions appropriately to ensure that the postgres user can access washington-latest.osm.pbf and flex.lua.
  • Note: if you get an error saying "peer authentication failed" when you run the last command, check out the solution at https://stackoverflow.com/a/26735105/1941353, but replace user 'postgres' with 'osmuser'.

5. Open the interactive query terminal by running: psql -d osm -U osmuser

  • Note: The interactive terminal that opens is where you'll run the PostgreSQL queries that the frontend generates.
  • You can confirm your database was set up correctly by running \dt. You should see four tables: shapes, linestrings, nodes, and spatial_ref_sys.