I want to convert spatial data from postgres to some basic formats (shp, geopackage, csv, xlsx, gml... ) using python gdal functionality. I read the answer to this question: Using ogr2ogr to convert GML to shapefile in Python?

I downloaded ogr2ogr.py and tried to convert data from postgres to some formats but I get an error saying:

KML driver failed to create /home/.../out.kml

This is the method I am using:

 ogr2ogr.main(["", "-f", "KML", "/home/.../out.kml",
              "PG:dbname='dbname' host='host' port=5432 user='username' password='password'",
              "-sql", "SELECT column1, column2 FROM schema.table"])
  • Is the path correct and do you have rights to write there?
    – user30184
    Feb 25, 2020 at 7:41
  • Yes, the path is correct. I gave permissions (read, write, execute) to everyone.
    – DrJacoby
    Feb 25, 2020 at 7:52
  • Three dots are strange "/home/.../out.kml" for a Unix file location.
    – huckfinn
    Dec 9, 2021 at 15:11

1 Answer 1


I'm not sure about the select column part but this should produce the entire table in kml:

ogr2ogr.main(["", "-f", "KML", "/home/.../out.kml",
              "PG:dbname='dbname' host='host' port=5432 user='username' password='password'","table"])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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