[GIS] Add new field in PostgreSQL row while using ogr2ogr for shapefile to PostgreSQL

ogr2ogrpostgresql

I am using the following ogr2ogr command for putting my shapefiles into PostgreSQL table. This command runs fine.

ogr2ogr.exe -append -lco GEOMETRY_NAME=geom -lco SCHEMA=public -f "PostgreSQL" PG:"host=localhost port=5433 user=postgres dbname=abc123 password=xxxxxx" myshapefile.shp -nln shapetable

Now I want is to add extra field/fields like (countryName=xyz / RoadName= street123/etc) while using the above command when exporting my shapefile to PostgreSQL table.

I was trying to find the answer in another link but it was for adding extra field while exporting GeoJSON into PostgreSQL table.

Best Answer

Read the ogr2ogr documentation http://www.gdal.org/ogr2ogr.html, especially section about -addfields

-addfields: (starting with GDAL 1.11) This is a specialized version of -append. Contrary to -append, -addfields has the effect of adding, to existing target layers, the new fields found in source layers. This option is useful when merging files that have non-strictly identical structures. This might not work for output formats that don't support adding fields to existing non-empty layers.

If this method does not suit you it is always possible to alter the schema with ogrinfo

ogrinfo PG:"dbname='test_db' host='localhost' port='5432' user='demouser' password='demopw'" -sql "ALTER TABLE test ADD COLUMN added_with_gdal TEXT"

Related Question