[GIS] Problems with ‘layer exists ‘ while running ogr2ogr

ogr2ogrpostgresql

I run command:

ogr2ogr -f PostgreSQL PG:"dbname=regional host=localhost user=*** password=***" Hsp_09_13.MIF -t_srs EPSG:3857 -nlt MULTIPOLYGON -overwrite -nln postcode_sector_boundaries

And I have error:

ERROR 1: Layer postcode_sector_boundaries already exists, CreateLayer
failed. Use the layer creation option OVERWRITE=YES to replace it.

So I run this command then:

ogr2ogr -f PostgreSQL PG:"dbname=regional host=localhost user=*** password=***" Hsp_09_13.MIF -t_srs EPSG:3857 -nlt MULTIPOLYGON -lco OVERWRITE=YES -overwrite -nln postcode_sector_boundaries

and got error:

ERROR 1: CREATE TABLE "public"."postcode_sector_boundaries" ( OGC_FID
SERIAL, PRIMARY KEY (OGC_FID) ) ERROR: relation
"postcode_sector_boundaries" already exists

So I remove table postcode_sector_boundaries and run once more last command and got:

ERROR 1: 'CREATE INDEX "postcode_sector_boundaries_geom_idx" ON
"public"."postcode_sector_boundaries" USING GIST ("wkb_geometry")'
failed for layer postcode_sector_boundaries, index creation has
failed.

postcode_sector_boundaries_geom_idx is unique key so I think that maybe my data are broken. I would like that ogr2ogr create table without that index but I can not find where definition for this create query can be. Any one knows? Or maybe there is another solution for my problem?

edit:

Ok, I know what was the problem – it was because there were some other tables using values from one I wanted recreate.

Best Answer

I've had some success with -append (to avoid recreating the table) and --config OGR_TRUNCATE YES (to truncate/empty the table before uploading new data)

ogr2ogr -append -f PostgreSQL PG:"dbname=regional host=localhost user=*** password=***" Hsp_09_13.MIF -t_srs EPSG:3857 -nlt MULTIPOLYGON -nln postcode_sector_boundaries --config OGR_TRUNCATE YES

(not tested with the above command but I use it regularly with other layers)