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.

  • I don't think you can change the database schema through ogr2ogr, but you can do it in a separate step through psql, i.e: psql -h localhost -p 5433 -U postgres -d abc123 -c "ALTER TABLE shapetable ADD COLUMN countryName TEXT DEFAULT 'xyz'" – Tom-db Mar 10 '15 at 7:41

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.