]> git.openstreetmap.org Git - nominatim.git/blob - docs/admin/Migration.md
remove Natural Earth dataset
[nominatim.git] / docs / admin / Migration.md
1 # Database Migrations
2
3 This page describes database migrations necessary to update existing databases
4 to newer versions of Nominatim.
5
6 SQL statements should be executed from the postgres commandline. Execute
7 `psql nominiatim` to enter command line mode.
8
9
10 ## 3.2.0 -> master
11
12 ### Natural Earth country boundaries no longer needed as fallback
13
14 ```
15 DROP TABLE country_naturalearthdata;
16 ```
17
18 Finally, update all SQL functions:
19
20 ```sh
21 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
22 ```
23
24
25 ## 3.1.0 -> 3.2.0
26
27 ### New reverse algorithm
28
29 The reverse algorithm has changed and requires new indexes. Run the following
30 SQL statements to create the indexes:
31
32 ```
33 CREATE INDEX idx_placex_geometry_reverse_lookupPoint
34   ON placex USING gist (geometry)
35   WHERE (name is not null or housenumber is not null or rank_address between 26 and 27)
36     AND class not in ('railway','tunnel','bridge','man_made')
37     AND rank_address >= 26 AND indexed_status = 0 AND linked_place_id is null;
38 CREATE INDEX idx_placex_geometry_reverse_lookupPolygon
39   ON placex USING gist (geometry)
40   WHERE St_GeometryType(geometry) in ('ST_Polygon', 'ST_MultiPolygon')
41     AND rank_address between 4 and 25 AND type != 'postcode'
42     AND name is not null AND indexed_status = 0 AND linked_place_id is null;
43 CREATE INDEX idx_placex_geometry_reverse_placeNode
44   ON placex USING gist (geometry)
45   WHERE osm_type = 'N' AND rank_search between 5 and 25
46     AND class = 'place' AND type != 'postcode'
47     AND name is not null AND indexed_status = 0 AND linked_place_id is null;
48 ```
49
50 You also need to grant the website user access to the `country_osm_grid` table:
51
52 ```
53 GRANT SELECT ON table country_osm_grid to "www-user";
54 ```
55
56 Replace the `www-user` with the user name of your website server if necessary.
57
58 You can now drop the unused indexes:
59
60 ```
61 DROP INDEX idx_placex_reverse_geometry;
62 ```
63
64 Finally, update all SQL functions:
65
66 ```sh
67 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
68 ```
69
70 ## 3.0.0 -> 3.1.0
71
72 ### Postcode Table
73
74 A new separate table for artificially computed postcode centroids was introduced.
75 Migration to the new format is possible but **not recommended**.
76
77 Create postcode table and indexes, running the following SQL statements:
78
79 ```sql
80 CREATE TABLE location_postcode
81   (place_id BIGINT, parent_place_id BIGINT, rank_search SMALLINT,
82    rank_address SMALLINT, indexed_status SMALLINT, indexed_date TIMESTAMP,
83    country_code varchar(2), postcode TEXT,
84    geometry GEOMETRY(Geometry, 4326));
85 CREATE INDEX idx_postcode_geometry ON location_postcode USING GIST (geometry);
86 CREATE UNIQUE INDEX idx_postcode_id ON location_postcode USING BTREE (place_id);
87 CREATE INDEX idx_postcode_postcode ON location_postcode USING BTREE (postcode);
88 GRANT SELECT ON location_postcode TO "www-data";
89 drop type if exists nearfeaturecentr cascade;
90 create type nearfeaturecentr as (
91   place_id BIGINT,
92   keywords int[],
93   rank_address smallint,
94   rank_search smallint,
95   distance float,
96   isguess boolean,
97   postcode TEXT,
98   centroid GEOMETRY
99 );
100 ```
101
102 Add postcode column to `location_area` tables with SQL statement:
103
104 ```sql
105 ALTER TABLE location_area ADD COLUMN postcode TEXT;
106 ```
107
108 Then reimport the functions:
109
110 ```sh
111 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
112 ```
113
114 Create appropriate triggers with SQL:
115
116 ```sql
117 CREATE TRIGGER location_postcode_before_update BEFORE UPDATE ON location_postcode
118     FOR EACH ROW EXECUTE PROCEDURE postcode_update();
119 ```
120
121 Finally populate the postcode table (will take a while):
122
123 ```sh
124 ./utils/setup.php --calculate-postcodes --index --index-noanalyse
125 ```
126
127 This will create a working database. You may also delete the old artificial
128 postcodes now. Note that this may be expensive and is not absolutely necessary.
129 The following SQL statement will remove them:
130
131 ```sql
132 DELETE FROM place_addressline a USING placex p
133  WHERE a.address_place_id = p.place_id and p.osm_type = 'P';
134 ALTER TABLE placex DISABLE TRIGGER USER;
135 DELETE FROM placex WHERE osm_type = 'P';
136 ALTER TABLE placex ENABLE TRIGGER USER;
137 ```