8

Is it possible to load a shapefile to a PostGIS database by replacing the content of the table with the new content of the shapefile WITHOUT DROPPING the table?

I want to do this using ogr2ogr.

In the past I have used shp2pgsql, which gives you the option of four flags (-drop|-append|-create|-prepare) but none of these options actually maintains the table and deletes the data.

Is there something like this in ogr2ogr?

nmtoken
  • 13,355
  • 5
  • 38
  • 87
user1919
  • 2,654
  • 2
  • 30
  • 70

1 Answers1

10

From the GDAL/OGR PG documentation:

Configuration Options

There are a variety of Configuration Options which help control the behavior of this driver.

  • ...

  • OGR_TRUNCATE: (GDAL >= 1.11) If set to "YES", the content of the table will be first erased with the SQL TRUNCATE command before inserting the first feature. This is an alternative to using the -overwrite flag of ogr2ogr, that avoids views based on the table to be destroyed. Typical use case: ogr2ogr -append PG:dbname=foo abc.shp --config OGR_TRUNCATE YES.

If you are stuck with GDAL < 1.11 and are unable to upgrade, you could issue a truncate SQL command before the ogr2ogr command:

psql -h pghost -p 5432 -U pguser -d pgdbname -c "TRUNCATE TABLE sometable;"
ogr2ogr etc...
user2856
  • 65,736
  • 6
  • 115
  • 196
  • 1
    Also for pre 1.11, you can run ogrinfo, providing the Truncate sql statement. This example is fgdb. http://gis.stackexchange.com/questions/42492/ogr2ogr-gdal-remove-features-in-filegdb-while-in-use-by-arcgis-server-map-serv – klewis Apr 04 '16 at 23:40