]> git.openstreetmap.org Git - nominatim.git/blob - docs/admin/Migration.md
Merge pull request #4163 from Itz-Agasta/category-support
[nominatim.git] / docs / admin / Migration.md
1 # Database Migrations
2
3 Nominatim offers automatic migrations for versions 4.3+. Please follow
4 the following steps:
5
6 * Stop any updates that are potentially running
7 * Update the backend: `pip install -U nominatim-db`
8 * Go to your project directory and run `nominatim admin --migrate`
9 * Update the frontend: `pip install -U nominatim-api`
10 * (optionally) Restart updates
11
12 Below you find additional migrations and hints about other structural and
13 breaking changes. **Please read them before running the migration.**
14
15 !!! note
16     If you are migrating from a version <4.3, you need to install 4.3
17     and migrate to 4.3 first. Then you can migrate to the current
18     version. It is strongly recommended to do a reimport instead.
19
20 ## 5.3.0 -> 5.4.0
21
22 ### The place_classtype tables are not used anymore
23
24 Category search now runs against the `categories` column of `placex`, so the
25 per-category `place_classtype_*` tables are not created or updated anymore.
26 The migration leaves the existing tables in place because the old frontend
27 code still queries them until you have restarted it with the new version.
28
29 Once all your frontends run the new version, you can drop the tables to free
30 the disk space. On a planet database that is a couple of hundred tables and
31 several GB. To generate the necessary SQL, run:
32
33 ```sql
34 SELECT format('DROP TABLE IF EXISTS %I;', tablename)
35   FROM pg_tables
36   WHERE schemaname = 'public' AND tablename LIKE 'place\_classtype\_%';
37 ```
38
39 The `--min` option of `nominatim special-phrases` only restricted which of
40 those tables were created. It has no effect anymore and is deprecated. It
41 will be removed in a future version.
42
43 ## 5.2.0 -> 5.3.0
44
45 #### Expensive table migrations
46
47 This version introduces new tables for postcodes, interpolations and
48 associatedStreet relations. Migration is possible but will take a bit
49 longer. It is recommended to take Nominatim offline while migrating to 5.3.
50
51 ## 5.1.0 -> 5.2.0
52
53 ### Lua import style: required extratags removed
54
55 Tags that are required by Nominatim as extratags are now always included
56 independent of what is defined in the style. The line
57
58     flex.add_for_extratags('required')
59
60 is no longer required in custom styles and will throw an error. Simply
61 remove the line from your style.
62
63 ## 4.5.0 -> 5.0.0
64
65 ### PHP frontend removed
66
67 The PHP frontend has been completely removed. Please switch to the Python
68 frontend.
69
70 Without the PHP code, the `nominatim refresh --website` command is no longer
71 needed. It currently omits a warning and does otherwise nothing. It will be
72 removed in later versions of Nominatim. So make sure you remove it from your
73 scripts.
74
75 ### CMake building removed
76
77 Nominatim can now only be installed via pip. Please follow the installation
78 instructions for the current version to change to pip.
79
80 ### osm2pgsql no longer vendored in
81
82 Nominatim no longer ships its own version of osm2pgsql. Please install a
83 stock version of osm2pgsql from your distribution. See the
84 [installation instruction for osm2pgsql](https://osm2pgsql.org/doc/install.html)
85 for details. A minimum version of 1.8 is required. The current stable versions
86 of Ubuntu and Debian already ship with an appropriate versions. For older
87 installations, you may have to compile a newer osm2pgsql yourself.
88
89 ### Legacy tokenizer removed
90
91 The `legacy` tokenizer is no longer enabled. This tokenizer has been superseded
92 by the `ICU` tokenizer a long time ago. In the unlikely case that your database
93 still uses the `legacy` tokenizer, you must reimport your database.
94
95 ### osm2pgsql style overhauled
96
97 There are some fundamental changes to how customized osm2pgsql styles should
98 be written. The changes are mostly backwards compatible, i.e. custom styles
99 should still work with the new implementation. The only exception is a
100 customization of the `process_tags()` function. This function is no longer
101 considered public and neither are the helper functions used in it.
102 They currently still work but will be removed at some point. If you have
103 been making changes to `process_tags`, please review your style and try
104 to switch to the new convenience functions.
105
106 For more information on the changes, see the
107 [pull request](https://github.com/osm-search/Nominatim/pull/3615)
108 and read the new
109 [customization documentation](https://nominatim.org/release-docs/latest/customize/Import-Styles/).
110
111 ## 4.4.0 -> 4.5.0
112
113 ### New structure for Python packages
114
115 The nominatim Python package has been split into `nominatim-db` and `nominatim-api`.
116 Any imports need to be adapted accordingly.
117
118 If you are running the Python frontend, change the server module from
119 `nominatim.server.falcon.server` to `nominatim_api.server.falcon.server`.
120
121 If you are using the Nominatim library, all imports need to be changed
122 from `nominatim.api.<module>` to `nominatim_api.<module>`.
123
124 If you have written custom tokenizers or sanitizers, the appropriate modules
125 are now found in `nominatim_db`.
126
127 ## 4.2.0 -> 4.3.0
128
129 ### New indexes for reverse lookup
130
131 The reverse lookup algorithm has changed slightly to improve performance.
132 This change needs a different index in the database. The required index
133 will be automatically build during migration. Until the new index is available
134 performance of the /reverse endpoint is significantly reduced. You should
135 therefore either remove traffic from the machine before attempting a
136 version update or create the index manually **before** starting the update
137 using the following SQL:
138
139 ```sql
140 CREATE INDEX IF NOT EXISTS idx_placex_geometry_reverse_lookupPlaceNode
141   ON placex USING gist (ST_Buffer(geometry, reverse_place_diameter(rank_search)))
142   WHERE rank_address between 4 and 25 AND type != 'postcode'
143     AND name is not null AND linked_place_id is null AND osm_type = 'N';
144 ```
145
146 ## 4.0.0 -> 4.1.0
147
148 ### ICU tokenizer is the new default
149
150 Nominatim now installs the [ICU tokenizer](../customize/Tokenizers.md#icu-tokenizer)
151 by default. This only has an effect on newly installed databases. When
152 updating older databases, it keeps its installed tokenizer. If you still
153 run with the legacy tokenizer, make sure to compile Nominatim with the
154 PostgreSQL module, see [Installation](Installation.md#building-nominatim).
155
156 ### geocodejson output changed
157
158 The `type` field of the geocodejson output has changed. It now contains
159 the address class of the object instead of the value of the OSM tag. If
160 your client has used the `type` field, switch them to read `osm_value`
161 instead.
162
163 ## 3.7.0 -> 4.0.0
164
165 ### NOMINATIM_PHRASE_CONFIG removed
166
167 Custom blacklist configurations for special phrases now need to be handed
168 with the `--config` parameter to `nominatim special-phrases`. Alternatively
169 you can put your custom configuration in the project directory in a file
170 named `phrase-settings.json`.
171
172 Version 3.8 also removes the automatic converter for the php format of
173 the configuration in older versions. If you are updating from Nominatim < 3.7
174 and still work with a custom `phrase-settings.php`, you need to manually
175 convert it into a json format.
176
177 ### PHP utils removed
178
179 The old PHP utils have now been removed completely. You need to switch to
180 the appropriate functions of the nominatim  command line tool. See
181 [Introducing `nominatim` command line tool](#introducing-nominatim-command-line-tool)
182 below.
183
184 ## 3.6.0 -> 3.7.0
185
186 ### New format and name of configuration file
187
188 The configuration for an import is now saved in a `.env` file in the project
189 directory. This file follows the dotenv format. For more information, see
190 the [installation chapter](Import.md#configuration-setup-in-env).
191
192 To migrate to the new system, create a new project directory, add the `.env`
193 file and port your custom configuration from `settings/local.php`. Most
194 settings are named similar and only have received a `NOMINATIM_` prefix.
195 Use the default settings in `settings/env.defaults` as a reference.
196
197 ### New location for data files
198
199 External data files for Wikipedia importance, postcodes etc. are no longer
200 expected to reside in the source tree by default. Instead they will be searched
201 in the project directory. If you have an automated setup script you must
202 either adapt the download location or explicitly set the location of the
203 files to the old place in your `.env`.
204
205 ### Introducing `nominatim` command line tool
206
207 The various php utilities have been replaced with a single `nominatim`
208 command line tool. Make sure to adapt any scripts. There is no direct 1:1
209 matching between the old utilities and the commands of nominatim CLI. The
210 following list gives you a list of nominatim sub-commands that contain
211 functionality of each script:
212
213 * ./utils/setup.php: `import`, `freeze`, `refresh`
214 * ./utils/update.php: `replication`, `add-data`, `index`, `refresh`
215 * ./utils/specialphrases.php: `special-phrases`
216 * ./utils/check_import_finished.php: `admin`
217 * ./utils/warm.php: `admin`
218 * ./utils/export.php: `export`
219
220 Try `nominatim <command> --help` for more information about each subcommand.
221
222 `./utils/query.php` no longer exists in its old form. `nominatim search`
223 provides a replacement but returns different output.
224
225 ### Switch to normalized house numbers
226
227 The housenumber column in the placex table uses now normalized version.
228 The automatic migration step will convert the column but this may take a
229 very long time. It is advisable to take the machine offline while doing that.
230
231 ## 3.5.0 -> 3.6.0
232
233 ### Change of layout of search_name_* tables
234
235 The table need a different index for nearest place lookup. Recreate the
236 indexes using the following shell script:
237
238 ```bash
239 for table in `psql -d nominatim -c "SELECT tablename FROM pg_tables WHERE tablename LIKE 'search_name_%'" -tA | grep -v search_name_blank`;
240 do
241     psql -d nominatim -c "DROP INDEX idx_${table}_centroid_place; CREATE INDEX idx_${table}_centroid_place ON ${table} USING gist (centroid) WHERE ((address_rank >= 2) AND (address_rank <= 25)); DROP INDEX idx_${table}_centroid_street; CREATE INDEX idx_${table}_centroid_street ON ${table} USING gist (centroid) WHERE ((address_rank >= 26) AND (address_rank <= 27))";
242 done
243 ```
244
245 ### Removal of html output
246
247 The debugging UI is no longer directly provided with Nominatim. Instead we
248 now provide a simple Javascript application. Please refer to
249 [Setting up the Nominatim UI](Setup-Nominatim-UI.md) for details on how to
250 set up the UI.
251
252 The icons served together with the API responses have been moved to the
253 nominatim-ui project as well. If you want to keep the `icon` field in the
254 response, you need to set `CONST_MapIcon_URL` to the URL of the `/mapicon`
255 directory of nominatim-ui.
256
257 ### Change order during indexing
258
259 When reindexing places during updates, there is now a different order used
260 which needs a different database index. Create it with the following SQL command:
261
262 ```sql
263 CREATE INDEX idx_placex_pendingsector_rank_address
264   ON placex
265   USING BTREE (rank_address, geometry_sector)
266   WHERE indexed_status > 0;
267 ```
268
269 You can then drop the old index with:
270
271 ```sql
272 DROP INDEX idx_placex_pendingsector;
273 ```
274
275 ### Unused index
276
277 This index has been unused ever since the query using it was changed two years ago. Saves about 12GB on a planet installation.
278
279 ```sql
280 DROP INDEX idx_placex_geometry_reverse_lookupPoint;
281 ```
282
283 ### Switching to dotenv
284
285 As part of the work changing the configuration format, the configuration for
286 the website is now using a separate configuration file. To create the
287 configuration file, run the following command after updating:
288
289 ```sh
290 ./utils/setup.php --setup-website
291 ```
292
293 ### Update SQL code
294
295 To update the SQL code to the leatest version run:
296
297 ```
298 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
299 ```
300
301 ## 3.4.0 -> 3.5.0
302
303 ### New Wikipedia/Wikidata importance tables
304
305 The `wikipedia_*` tables have a new format that also includes references to
306 Wikidata. You need to update the computation functions and the tables as
307 follows:
308
309   * download the new Wikipedia tables as described in the import section
310   * reimport the tables: `./utils/setup.php --import-wikipedia-articles`
311   * update the functions: `./utils/setup.php --create-functions --enable-diff-updates`
312   * create a new lookup index:
313 ```sql
314 CREATE INDEX idx_placex_wikidata
315   ON placex
316   USING BTREE ((extratags -> 'wikidata'))
317   WHERE extratags ? 'wikidata'
318     AND class = 'place'
319     AND osm_type = 'N'
320     AND rank_search < 26;
321 ```
322   * compute importance: `./utils/update.php --recompute-importance`
323
324 The last step takes about 10 hours on the full planet.
325
326 Remove one function (it will be recreated in the next step):
327
328 ```sql
329 DROP FUNCTION create_country(hstore,character varying);
330 ```
331
332 Finally, update all SQL functions:
333
334 ```sh
335 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
336 ```
337
338 ## 3.3.0 -> 3.4.0
339
340 ### Reorganisation of location_area_country table
341
342 The table `location_area_country` has been optimized. You need to switch to the
343 new format when you run updates. While updates are disabled, run the following
344 SQL commands:
345
346 ```sql
347 CREATE TABLE location_area_country_new AS
348   SELECT place_id, country_code, geometry FROM location_area_country;
349 DROP TABLE location_area_country;
350 ALTER TABLE location_area_country_new RENAME TO location_area_country;
351 CREATE INDEX idx_location_area_country_geometry ON location_area_country USING GIST (geometry);
352 CREATE INDEX idx_location_area_country_place_id ON location_area_country USING BTREE (place_id);
353 ```
354
355 Finally, update all SQL functions:
356
357 ```sh
358 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
359 ```
360
361 ## 3.2.0 -> 3.3.0
362
363 ### New database connection string (DSN) format
364
365 Previously database connection setting (`CONST_Database_DSN` in `settings/*.php`) had the format
366
367    * (simple) `pgsql://@/nominatim`
368    * (complex) `pgsql://johndoe:secret@machine1.domain.com:1234/db1`
369
370 The new format is
371
372    * (simple) `pgsql:dbname=nominatim`
373    * (complex) `pgsql:dbname=db1;host=machine1.domain.com;port=1234;user=johndoe;password=secret`
374
375 ### Natural Earth country boundaries no longer needed as fallback
376
377 ```sql
378 DROP TABLE country_naturalearthdata;
379 ```
380
381 Finally, update all SQL functions:
382
383 ```sh
384 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
385 ```
386
387 ### Configurable Address Levels
388
389 The new configurable address levels require a new table. Create it with the
390 following command:
391
392 ```sh
393 ./utils/update.php --update-address-levels
394 ```
395
396 ## 3.1.0 -> 3.2.0
397
398 ### New reverse algorithm
399
400 The reverse algorithm has changed and requires new indexes. Run the following
401 SQL statements to create the indexes:
402
403 ```sql
404 CREATE INDEX idx_placex_geometry_reverse_lookupPoint
405   ON placex
406   USING gist (geometry)
407   WHERE (name IS NOT null or housenumber IS NOT null or rank_address BETWEEN 26 AND 27)
408     AND class NOT IN ('railway','tunnel','bridge','man_made')
409     AND rank_address >= 26
410     AND indexed_status = 0
411     AND linked_place_id IS null;
412 CREATE INDEX idx_placex_geometry_reverse_lookupPolygon
413   ON placex USING gist (geometry)
414   WHERE St_GeometryType(geometry) in ('ST_Polygon', 'ST_MultiPolygon')
415     AND rank_address between 4 and 25
416     AND type != 'postcode'
417     AND name is not null
418     AND indexed_status = 0
419     AND linked_place_id is null;
420 CREATE INDEX idx_placex_geometry_reverse_placeNode
421   ON placex USING gist (geometry)
422   WHERE osm_type = 'N'
423     AND rank_search between 5 and 25
424     AND class = 'place'
425     AND type != 'postcode'
426     AND name is not null
427     AND indexed_status = 0
428     AND linked_place_id is null;
429 ```
430
431 You also need to grant the website user access to the `country_osm_grid` table:
432
433 ```sql
434 GRANT SELECT ON table country_osm_grid to "www-user";
435 ```
436
437 Replace the `www-user` with the user name of your website server if necessary.
438
439 You can now drop the unused indexes:
440
441 ```sql
442 DROP INDEX idx_placex_reverse_geometry;
443 ```
444
445 Finally, update all SQL functions:
446
447 ```sh
448 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
449 ```
450
451 ## 3.0.0 -> 3.1.0
452
453 ### Postcode Table
454
455 A new separate table for artificially computed postcode centroids was introduced.
456 Migration to the new format is possible but **not recommended**.
457
458 Create postcode table and indexes, running the following SQL statements:
459
460 ```sql
461 CREATE TABLE location_postcode
462   (place_id BIGINT, parent_place_id BIGINT, rank_search SMALLINT,
463    rank_address SMALLINT, indexed_status SMALLINT, indexed_date TIMESTAMP,
464    country_code varchar(2), postcode TEXT,
465    geometry GEOMETRY(Geometry, 4326));
466 CREATE INDEX idx_postcode_geometry ON location_postcode USING GIST (geometry);
467 CREATE UNIQUE INDEX idx_postcode_id ON location_postcode USING BTREE (place_id);
468 CREATE INDEX idx_postcode_postcode ON location_postcode USING BTREE (postcode);
469 GRANT SELECT ON location_postcode TO "www-data";
470 DROP TYPE IF EXISTS nearfeaturecentr CASCADE;
471 CREATE TYPE nearfeaturecentr AS (
472   place_id BIGINT,
473   keywords int[],
474   rank_address smallint,
475   rank_search smallint,
476   distance float,
477   isguess boolean,
478   postcode TEXT,
479   centroid GEOMETRY
480 );
481 ```
482
483 Add postcode column to `location_area` tables with SQL statement:
484
485 ```sql
486 ALTER TABLE location_area ADD COLUMN postcode TEXT;
487 ```
488
489 Then reimport the functions:
490
491 ```sh
492 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
493 ```
494
495 Create appropriate triggers with SQL:
496
497 ```sql
498 CREATE TRIGGER location_postcode_before_update BEFORE UPDATE ON location_postcode
499     FOR EACH ROW EXECUTE PROCEDURE postcode_update();
500 ```
501
502 Finally populate the postcode table (will take a while):
503
504 ```sh
505 ./utils/setup.php --calculate-postcodes --index --index-noanalyse
506 ```
507
508 This will create a working database. You may also delete the old artificial
509 postcodes now. Note that this may be expensive and is not absolutely necessary.
510 The following SQL statement will remove them:
511
512 ```sql
513 DELETE FROM place_addressline a USING placex p
514  WHERE a.address_place_id = p.place_id and p.osm_type = 'P';
515 ALTER TABLE placex DISABLE TRIGGER USER;
516 DELETE FROM placex WHERE osm_type = 'P';
517 ALTER TABLE placex ENABLE TRIGGER USER;
518 ```