Skip to main content
Tweeted twitter.com/StackGIS/status/1144938612573839367
edited tags
Link
Ian Turton
  • 83.2k
  • 6
  • 89
  • 189
Source Link
Theo F
  • 1.9k
  • 14
  • 35

Running gdal ogr2ogr command from python

I have an ogr2ogr command which uploads an ESRI File Geodatabase to a PostgreSQL database, updating/overwriting the existing data:

ogr2ogr -f "PostgreSQL" PG:"host=hosting_url port=5432 dbname=database_name user=username password=password" -lco SCHEMA=schema_name "D:\data\Data.gdb" -overwrite -progress -lco OVERWRITE=YES --config PG_USE_COPY YES

This works as expected when run from OSGeo4W Shell (installed with my QGIS installation). However, I'd like to automate this command to run at the same time as an existing python script I have. So my challenge is to run the ogr2ogr command from python. How can I make it compatible?

I've tried:

import os, subprocess
command = ["C:\\Program Files\\QGIS 2.18\\bin\\ogr2ogr.exe", 
        "-f", "PostgreSQL", "PG:\"host=hosting_url port=5432 dbname=database_name user=username password=password\"", "-lco", "SCHEMA=schema_name", "D:\data\Data.gdb", "-overwrite", "-progress", "-lco", "OVERWRITE=YES", "--config", "PG_USE_COPY", "YES"]

I adapted this code from the top answer here: Execute ogr2ogr from python

When I run it I get the error 'returned non-zero exit status 1.'