from pathlib import Path
import psutil
+import psycopg2
from ..db.connection import connect, get_pg_env
from ..db import utils as db_utils
LOG = logging.getLogger()
+def setup_database_skeleton(dsn, data_dir, no_partitions, rouser=None):
+ """ Create a new database for Nominatim and populate it with the
+ essential extensions and data.
+ """
+ LOG.warning('Creating database')
+ create_db(dsn, rouser)
+
+ LOG.warning('Setting up database')
+ with connect(dsn) as conn:
+ setup_extensions(conn)
+
+ LOG.warning('Loading basic data')
+ import_base_data(dsn, data_dir, no_partitions)
+
+
def create_db(dsn, rouser=None):
""" Create a new database for the given DSN. Fails when the database
already exists or the PostgreSQL version is too old.
raise UsageError('PostGIS version is too old.')
-def install_module(src_dir, project_dir, module_dir):
+def install_module(src_dir, project_dir, module_dir, conn=None):
""" Copy the normalization module from src_dir into the project
directory under the '/module' directory. If 'module_dir' is set, then
use the module from there instead and check that it is accessible
The function detects when the installation is run from the
build directory. It doesn't touch the module in that case.
+
+ If 'conn' is given, then the function also tests if the module
+ can be access via the given database.
"""
if not module_dir:
module_dir = project_dir / 'module'
else:
LOG.info("Using custom path for database module at '%s'", module_dir)
- return module_dir
-
-
-def check_module_dir_path(conn, path):
- """ Check that the normalisation module can be found and executed
- from the given path.
- """
- with conn.cursor() as cur:
- cur.execute("""CREATE FUNCTION nominatim_test_import_func(text)
- RETURNS text AS '{}/nominatim.so', 'transliteration'
- LANGUAGE c IMMUTABLE STRICT;
- DROP FUNCTION nominatim_test_import_func(text)
- """.format(path))
+ if conn is not None:
+ with conn.cursor() as cur:
+ try:
+ cur.execute("""CREATE FUNCTION nominatim_test_import_func(text)
+ RETURNS text AS '{}/nominatim.so', 'transliteration'
+ LANGUAGE c IMMUTABLE STRICT;
+ DROP FUNCTION nominatim_test_import_func(text)
+ """.format(module_dir))
+ except psycopg2.DatabaseError as err:
+ LOG.fatal("Error accessing database module: %s", err)
+ raise UsageError("Database module cannot be accessed.") from err
def import_base_data(dsn, sql_dir, ignore_partitions=False):
conn.commit()
-def import_osm_data(osm_file, options, drop=False):
+def import_osm_data(osm_file, options, drop=False, ignore_errors=False):
""" Import the given OSM file. 'options' contains the list of
default settings for osm2pgsql.
"""
run_osm2pgsql(options)
with connect(options['dsn']) as conn:
- with conn.cursor() as cur:
- cur.execute('SELECT * FROM place LIMIT 1')
- if cur.rowcount == 0:
- raise UsageError('No data imported by osm2pgsql.')
+ if not ignore_errors:
+ with conn.cursor() as cur:
+ cur.execute('SELECT * FROM place LIMIT 1')
+ if cur.rowcount == 0:
+ raise UsageError('No data imported by osm2pgsql.')
if drop:
conn.drop_table('planet_osm_nodes')
cur.execute('TRUNCATE location_property_tiger')
cur.execute('TRUNCATE location_property_osmline')
cur.execute('TRUNCATE location_postcode')
- cur.execute('TRUNCATE search_name')
- cur.execute('DROP SEQUENCE seq_place')
+ if conn.table_exists('search_name'):
+ cur.execute('TRUNCATE search_name')
+ cur.execute('DROP SEQUENCE IF EXISTS seq_place')
cur.execute('CREATE SEQUENCE seq_place start 100000')
cur.execute("""SELECT tablename FROM pg_tables