Accessing non-spatial PostgreSQL tables using OGR Python bindings?
You can execute any arbitrary SQL from the connection. For example, to get data from spatial_ref_sys
, which is a non-spatial table:
from osgeo import ogr
conn = ogr.Open('PG:dbname=postgis user=postgres')
sql = 'SELECT * FROM spatial_ref_sys LIMIT 10;'
for row in conn.ExecuteSQL(sql):
print('%s:%d | %s'%(row.auth_name, row.auth_srid, row.proj4text))
For reference, it is possible to set GDAL configuration options using gdal.SetConfigOption().
To list all tables:
import osgeo.gdal as gdal
import osgeo.ogr as ogr
gdal.SetConfigOption("PG_LIST_ALL_TABLES", "YES")
conn = ogr.Open("PG: #params")
for layer in conn:
print layer.GetName()
# Will print all tables including non-spatial ones.
You can see further examples of gdal.SetConfigOption in the GDAL/OGR test suite PostGIS module: http://svn.osgeo.org/gdal/trunk/autotest/ogr/ogr_pg.py