]> git.openstreetmap.org Git - nominatim.git/blob - docs/admin/Migration.md
Merge pull request #884 from lonvia/docs-tomkdocs
[nominatim.git] / docs / admin / Migration.md
1 Database Migrations
2 ===================
3
4 This page describes database migrations necessary to update existing databases
5 to newer versions of Nominatim.
6
7 SQL statements should be executed from the postgres commandline. Execute
8 `psql nominiatim` to enter command line mode.
9
10 # 3.0.0 -> 3.1.0
11
12 ### Postcode Table
13
14 A new separate table for artificially computed postcode centroids was introduced.
15 Migration to the new format is possible but **not recommended**.
16
17 Create postcode table and indexes, running the following SQL statements:
18
19 ```sql
20 CREATE TABLE location_postcode
21   (place_id BIGINT, parent_place_id BIGINT, rank_search SMALLINT,
22    rank_address SMALLINT, indexed_status SMALLINT, indexed_date TIMESTAMP,
23    country_code varchar(2), postcode TEXT,
24    geometry GEOMETRY(Geometry, 4326));
25 CREATE INDEX idx_postcode_geometry ON location_postcode USING GIST (geometry);
26 CREATE UNIQUE INDEX idx_postcode_id ON location_postcode USING BTREE (place_id);
27 CREATE INDEX idx_postcode_postcode ON location_postcode USING BTREE (postcode);
28 GRANT SELECT ON location_postcode TO "www-data";
29 ```
30
31 Add postcode column to `location_area` tables with SQL statement:
32
33 ```sql
34 ALTER TABLE location_area ADD COLUMN postcode TEXT;
35 ```
36
37 Then reimport the functions:
38
39 ```sh
40 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
41 ```
42
43 Create appropriate triggers with SQL:
44
45 ```sql
46 CREATE TRIGGER location_postcode_before_update BEFORE UPDATE ON location_postcode
47     FOR EACH ROW EXECUTE PROCEDURE postcode_update();
48 ```
49
50 Finally populate the postcode table (will take a while):
51
52 ```sh
53 ./utils/setup.php --calculate-postcodes --index --index-noanalyse
54 ```
55
56 This will create a working database. You may also delete the old artificial
57 postcodes now. Note that this may be expensive and is not absolutely necessary.
58 The following SQL statement will remove them:
59
60 ```sql
61 DELETE FROM place_addressline a USING placex p
62  WHERE a.address_place_id = p.place_id and p.osm_type = 'P';
63 ALTER TABLE placex DISABLE TRIGGER USER;
64 DELETE FROM placex WHERE osm_type = 'P';
65 ALTER TABLE placex ENABLE TRIGGER USER;
66 ```