I have been successfully using the Gdaltools module in Python to export from PostGIS to shapefile using:

import gdaltools

ogr = gdaltools.ogr2ogr()

conn = gdaltools.PgConnectionString(host=HOST, port=5432, dbname=db, schema=scema, user=user, password=PASSWORD)

ogr.set_input(conn, table_name="tablename", srs="EPSG:4326")



I would like to add an SQL query to the mix (since the whole table is currently exporting) and not sure where it should go.

1 Answer 1


I think you should be able to feed an SQL query to this ogr module since it's a python wrapper of ogr2ogr and as you can see in this doc. You can give an sql query with the command "-clipsrcsql", but I don't know if it's in the Gdaltools python module.

also, you can use the '-select' argument if you want some field only

so maybe ? :

ogr.set_input(conn, table_name="tablename", srs="EPSG:4326", clipsrcsql="your query")

However I found no example of anything that's say it should work ...

never used it and never seen it, did you try ?

  • Thank you for the suggestion @Maximilien-jaffrès, I did try the clipsrcsql but as you predicted, it did not recognize it, so its probably not part of the Gdaltools python module. Oct 28, 2019 at 15:14
  • mhh, if we go KISS, you can try to use os.system and write the ogr2ogr command line ... Oct 28, 2019 at 15:33
  • The --clipsrcsql argument is actually to specify an SQL statement for a clipping geometry not the source. It's the -sql argument you'll want to emulate
    – Ali
    Mar 26, 2020 at 20:13

Your Answer

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

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