public function createDB()
{
info('Create DB');
- $bExists = true;
- try {
- $oDB = new \Nominatim\DB;
- $oDB->connect();
- } catch (\Nominatim\DatabaseError $e) {
- $bExists = false;
- }
+ $oDB = new \Nominatim\DB;
- if ($bExists) {
+ if ($oDB->databaseExists()) {
fail('database already exists ('.CONST_Database_DSN.')');
}
$fPostgresVersion = $this->oDB->getPostgresVersion();
echo 'Postgres version found: '.$fPostgresVersion."\n";
- if ($fPostgresVersion < 9.01) {
- fail('Minimum supported version of Postgresql is 9.1.');
+ if ($fPostgresVersion < 9.03) {
+ fail('Minimum supported version of Postgresql is 9.3.');
}
$this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS hstore');
$this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis');
- // For extratags and namedetails the hstore_to_json converter is
- // needed which is only available from Postgresql 9.3+. For older
- // versions add a dummy function that returns nothing.
- $iNumFunc = chksql($this->oDB->getOne("select count(*) from pg_proc where proname = 'hstore_to_json'"));
-
- if ($iNumFunc == 0) {
- $this->pgsqlRunScript("create function hstore_to_json(dummy hstore) returns text AS 'select null::text' language sql immutable");
- warn('Postgresql is too old. extratags and namedetails API not available.');
- }
-
-
$fPostgisVersion = $this->oDB->getPostgisVersion();
echo 'Postgis version found: '.$fPostgisVersion."\n";
- if ($fPostgisVersion < 2.1) {
- // Functions were renamed in 2.1 and throw an annoying deprecation warning
- $this->pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint');
- $this->pgsqlRunScript('ALTER FUNCTION ST_Line_Locate_Point(geometry, geometry) RENAME TO ST_LineLocatePoint');
- }
if ($fPostgisVersion < 2.2) {
- $this->pgsqlRunScript('ALTER FUNCTION ST_Distance_Spheroid(geometry, geometry, spheroid) RENAME TO ST_DistanceSpheroid');
+ echo "Minimum required Postgis version 2.2\n";
+ exit(1);
}
- $i = chksql($this->oDB->getOne("select count(*) from pg_user where usename = '".CONST_Database_Web_User."'"));
+ $i = $this->oDB->getOne("select count(*) from pg_user where usename = '".CONST_Database_Web_User."'");
if ($i == 0) {
echo "\nERROR: Web user '".CONST_Database_Web_User."' does not exist. Create it with:\n";
echo "\n createuser ".CONST_Database_Web_User."\n\n";
}
// Try accessing the C module, so we know early if something is wrong
- if (!checkModulePresence()) {
- fail('error loading nominatim.so module');
- }
+ checkModulePresence(); // raises exception on failure
if (!file_exists(CONST_ExtraDataPath.'/country_osm_grid.sql.gz')) {
echo 'Error: you need to download the country_osm_grid first:';
exit(1);
}
$this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
- $this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql.gz');
+ $this->pgsqlRunScriptFile(CONST_ExtraDataPath.'/country_osm_grid.sql.gz');
$this->pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql');
+ $this->pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode_table.sql');
$sPostcodeFilename = CONST_BasePath.'/data/gb_postcode_data.sql.gz';
if (file_exists($sPostcodeFilename)) {
$this->pgsqlRunScriptFile($sPostcodeFilename);
} else {
- warn('optional external UK postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
+ warn('optional external GB postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
}
- if (CONST_Use_Extra_US_Postcodes) {
- $this->pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql');
+ $sPostcodeFilename = CONST_BasePath.'/data/us_postcode_data.sql.gz';
+ if (file_exists($sPostcodeFilename)) {
+ $this->pgsqlRunScriptFile($sPostcodeFilename);
+ } else {
+ warn('optional external US postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
}
if ($this->bNoPartitions) {
$this->runWithPgEnv($osm2pgsql);
- if (!$this->sIgnoreErrors && !chksql($this->oDB->getRow('select * from place limit 1'))) {
+ if (!$this->sIgnoreErrors && !$this->oDB->getRow('select * from place limit 1')) {
fail('No Data');
}
}
{
info('Create Functions');
- // Try accessing the C module, so we know eif something is wrong
- // update.php calls this function
- if (!checkModulePresence()) {
- fail('error loading nominatim.so module');
- }
+ // Try accessing the C module, so we know early if something is wrong
+ checkModulePresence(); // raises exception on failure
+
$this->createSqlFunctions();
}
echo '.';
$sSQL = 'select distinct partition from country_name';
- $aPartitions = chksql($this->oDB->getCol($sSQL));
+ $aPartitions = $this->oDB->getCol($sSQL);
if (!$this->bNoPartitions) $aPartitions[] = 0;
foreach ($aPartitions as $sPartition) {
{
info('Import Tiger data');
+ $aFilenames = glob(CONST_Tiger_Data_Path.'/*.sql');
+ info('Found '.count($aFilenames).' SQL files in path '.CONST_Tiger_Data_Path);
+ if (empty($aFilenames)) return;
+
$sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql');
$sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
$sTemplate = $this->replaceTablespace(
pg_ping($aDBInstances[$i]);
}
- foreach (glob(CONST_Tiger_Data_Path.'/*.sql') as $sFile) {
+ foreach ($aFilenames as $sFile) {
echo $sFile.': ';
$hFile = fopen($sFile, 'r');
$sSQL = fgets($hFile, 100000);
$sSQL .= ' GROUP BY country_code, pc';
$this->pgExec($sSQL);
- if (CONST_Use_Extra_US_Postcodes) {
- // only add postcodes that are not yet available in OSM
- $sSQL = 'INSERT INTO location_postcode';
- $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
- $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
- $sSQL .= ' ST_SetSRID(ST_Point(x,y),4326)';
- $sSQL .= ' FROM us_postcode WHERE postcode NOT IN';
- $sSQL .= ' (SELECT postcode FROM location_postcode';
- $sSQL .= " WHERE country_code = 'us')";
- $this->pgExec($sSQL);
- }
+ // only add postcodes that are not yet available in OSM
+ $sSQL = 'INSERT INTO location_postcode';
+ $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
+ $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
+ $sSQL .= ' ST_SetSRID(ST_Point(x,y),4326)';
+ $sSQL .= ' FROM us_postcode WHERE postcode NOT IN';
+ $sSQL .= ' (SELECT postcode FROM location_postcode';
+ $sSQL .= " WHERE country_code = 'us')";
+ $this->pgExec($sSQL);
// add missing postcodes for GB (if available)
$sSQL = 'INSERT INTO location_postcode';
);
$aDropTables = array();
- $aHaveTables = chksql($this->oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'"));
+ $aHaveTables = $this->oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'");
foreach ($aHaveTables as $sTable) {
$bFound = false;
}
foreach ($aDropTables as $sDrop) {
if ($this->bVerbose) echo "Dropping table $sDrop\n";
- $this->oDB->exec("DROP TABLE $sDrop CASCADE");
- // ignore warnings/errors as they might be caused by a table having
- // been deleted already by CASCADE
+ $this->oDB->exec("DROP TABLE IF EXISTS $sDrop CASCADE");
}
if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
private function pgsqlRunDropAndRestore($sDumpFile)
{
- $sCMD = 'pg_restore -p '.$this->aDSNInfo['port'].' -d '.$this->aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
+ $sCMD = 'pg_restore -p '.$this->aDSNInfo['port'].' -d '.$this->aDSNInfo['database'].' --no-owner -Fc --clean '.$sDumpFile;
+ if ($this->oDB->getPostgresVersion() >= 9.04) {
+ $sCMD .= ' --if-exists';
+ }
if (isset($this->aDSNInfo['hostspec'])) {
$sCMD .= ' -h '.$this->aDSNInfo['hostspec'];
}