OpenStreetMap is a crowdsourced database which has editions every second, due this the database that you downloaded a month ago is outdated. To solve this problem I did this little tutorial.

Initial database load

The first step is to load the file to the Postgres. To do this we will use osm2pgsql tool. You can install it with this command

apt-get install osm2pgsql

After this, we can download the actual database from Geofabrik. In this example we will use the data from Andorra:


Once we have the osm data we load it to the Postgres

createdb osm #Create the database on Postgres
psql -d osm -c "CREATE EXTENSION postgis" # Creates the postgis extension
psql -d osm -c "CREATE EXTENSION hstore" # Creates the hstore extension
osm2pgsql -d osm -s -C 4096 andorra-latest.osm.pbf --hstore # Loads the data 

We need one more step to initialize the replication. In this case we will initialize the osmosis directory on $HOME/osmosis/

mkdir -p $HOME/osmosis/andorra

osmosis --rrii workingDirectory=$HOME/osmosis/andorra
sed -i 's!baseUrl=!baseUrl=!' $HOME/osmosis/andorra/configuration.txt
wget -O "$HOME/osmosis/andorra/state.txt"

Cron task

After we load the database we need to update the database periodically. To do this on Linux we use cron. First we create a scprit, for example on $HOME/ with this commands:

osmosis --read-replication-interval workingDirectory="$HOME/osmosis/andorra" --simplify-change --write-xml-change $HOME/andorra_changes.osm
/usr/local/bin/osm2pgsql --append -s -C 3000 -G --hstore -d osm $HOME/andorra_changes.osm

Remember to add execution permisions to the script

chmod +x $HOME/

To configure the task once a day we can add this line on cron

0 7 * * * $HOME/