[gdal-dev] Python bindings and the Postgres driver

Howard Butler hobu.inc at gmail.com
Tue May 6 11:35:14 EDT 2008


On May 6, 2008, at 10:16 AM, Pat Blair wrote:

> Hello.  I am using GDAL/OGR from Python 2.5 and believe that I am  
> having trouble using the PostgreSQL driver.  (The problem I am  
> having is similar or identical to one posted previously, but for  
> which I can find no answer: http://www.nabble.com/gdal-Python-bindings-and-postgis-question-td15705650.html.)
>
> I am using Python 2.5 on Windows.  I have C:\gdalwin32-1.5 in place,  
> and have the Postgres drivers in C:\gdalwin32-1.5\bin\gdalplugins  
> (libpq.dll, and ogr_PG.dll).  I have also installed the Python  
> bindings.  When I run the ogrinfo utility from a command line, I can  
> use the Postgres driver with no apparent problem.
>
> For example, when I run...
>
> ogrinfo PG:"host=localhost user=postgres password=sergtsop  
> dbname=arcturus" -al -summary
>
> I get results...
>
> INFO: Open of `PG:host=localhost user=postgres password=password  
> dbname=mydb'
>       using driver `PostgreSQL' successful.
>
> Layer name: streets
> Geometry: Line String
> Feature Count: 19187
> Extent: (-77.116646, 38.793179) - (-76.909538, 38.995243)
> Layer SRS WKT:
> [...etc...]
>
> However, from within Python, the ogr module doesn't seem to be able  
> to find the 'PostgreSQL' driver.  Below I have pasted in text from  
> my Python console.  You can see that I can get the 'ESRI Shapefile'  
> driver; but if I try to get 'PostgreSQL' driver, I get None back.
>
> >>> from osgeo import ogr
> >>> ogrEsriDriver = ogr.GetDriverByName('ESRI Shapefile')
> >>> print ogrEsriDriver
> <osgeo.ogr.Driver; proxy of <Swig Object of type 'OGRDriverShadow *'  
> at 0xaf3a78...
> >>> ogrPgDriver = ogr.GetDriverByName('PostgreSQL')
> >>> print ogrPgDriver
> None
> >>>
>
> So, I believe that GDAL/OGR are installed and working, and that the  
> Python bindings are generally working.  But is there another step I  
> must take to help the Python bindings find the Postgres driver?
>

Pat,

I think you need to set the GDAL_DRIVER_PATH environment variable  
*before* Python is run so that GDAL can know where to find its plugins.

Running from a cmd.exe...

set GDAL_DRIVER_PATH=C:\gdalwin32-1.5\bin\gdalplugins
c:\Python25\python.exe
 >>> from osgeo import ogr
 >>> ogrPgDriver = ogr.GetDriverByName('PostgreSQL')
 >>> print ogrPgDriver

Alternatively, setting this as a system environment variable may be  
one way to avoid having to manually set it every time you wish to run  
Python.

Hope this helps,

Howard


More information about the gdal-dev mailing list