6

I have a windows server (Microsoft Windows Server 2003) with:

If I do

ogrinfo PG:"dbname=postgis_tpl user=postgres password=xxxxxx"

connection with the database is successful:

INFO: Open of `PG:dbname=postgis_tpl user=postgres password=xxxxxx'
  using driver `PostgreSQL' successful.
1: world_merc (Multi Polygon)

However, from the Python shell, if I do:

>>> from osgeo import ogr
>>> print ogr.Open("PG:dbname=postgis_tpl user=postgres password=xxxxxx")

I get:

None

and:

>>> print ogr.GetDriverByName("postgresql")

gives me:

None

and if I list all the drivers available via the Python bindings:

>>> for i in range(0,ogr.GetDriverCount()):
...     print ogr.GetDriver(i).name

the following drivers are listed (PostgreSQL is missing):

ESRI Shapefile, MapInfo File, UK .NTF, SDTS, TIGER, S57, DGN, VRT, REC, Memory, BNA, CSV, NAS, GML, GPX, KML, GeoJSON, GMT, SQLite, ODBC, PGeo, MSSQLSpatial, PCIDSK, XPlane, AVCBin, AVCE00, DXF, Geoconcept, GeoRSS, GPSTrackMaker, VFK, PGDump, GPSBabel, SUA, OpenAir, PDS, WFS, HTF, AeronavFAA

My environment variables are (I think) set correctly:

GDAL_DRIVER_PATH = C:\Program Files\GDAL\gdalplugins

(There's nothing that looks like the PostgreSQL driver in the gdal-plugins folder anyway).

GDAL_DATA = C:\Program Files\GDAL\gdal-data

and

C:\Program Files\GDAL

is in my PATH (GDAL is installed at C:\Program Files\GDAL).

So what am I missing? Why can't Python see the PostgreSQL driver? Do I need to install its bindings separately?

2 Answers 2

3

Have you logged off or rebooted your server since setting the environment variables? The changes only come into effect once you do this.

I often now just set these in my Python scripts manually to avoid conflicts / reliancy on Windows variables. You could try something similar in your script or IDLE:

import os
os.environ['PATH'] = "C:\\Program Files\\GDAL" + ';' + os.environ['PATH']
os.environ['GDAL_DRIVER_PATH'] = "C:\\Program Files\\GDAL\\gdalplugins"
os.environ['GDAL_DATA'] = "C:\\Program Files\\GDAL\\gdal-data"

Assuming this still fails..

The PostGIS driver requires the libpq.dll (the postgres client library). You can test the GDAL/OGR dll with depends.exe to see if it is missing.

I would have assumed it had been compiled with this as the Info Page lists the PostGIS driver. It is present in a recent MapServer download I took from the same site.

libpq.dll

However it is worth checking, and making sure it is in your PATH variable.

After checking the Info Page again it notes GDAL was built with the postgresql-8.3.7 client library. As you are using PostgreSQL 9.04-1 this could be a problem.

In my MapServer download there is a:

gdal\plugins-optional\ogr_PG.dll

I believe this is required for using the PostGIS plugin with OGR, so moving this to your PATH could well allow you to use the driver.

As you say in your comments this does not seem to be included in the package. To resolve the issue, download a MapServer package, which does contain this file. Once I added this to the C:\Program Files\GDAL\gdalplugins folder with the exact same installs as you it worked:

enter image description here

enter image description here

7
  • Thanks for the response but no, I'd already tried rebooting. I did try your suggestion re defining the path directly from Python too but no success I'm afraid.
    – fod
    Commented Aug 18, 2011 at 15:37
  • Unfortunately this is a development server that I've been grudgingly allowed to use by my employers to develop a proof-of-concept application. It has no internet connectivity and I have to jump through a lot of hoops every time I want something added to my stack, so its not easy for me to get, for instance, dependency walker installed. I think you might be onto something with the PostgreSQL version but note that the driver is there and works fine when accessed directly, and only fails to appear when attempting access via the Python bindings.
    – fod
    Commented Aug 18, 2011 at 16:16
  • @fod - it should be in the GDAL folder. Try a Windows search. If it is not in a folder on your path then add this folder to the path also. Commented Aug 18, 2011 at 16:18
  • 1
    @fod also try moving ogr_PG.dll to your PATH Commented Aug 18, 2011 at 16:30
  • 1
    @fod - grab a MapServer package and take the ogr_pg.dll file from it. All seems to work with this in place. Commented Aug 18, 2011 at 17:02
2

The PostgreSQL OGR driver is not compiled into GDAL by default, so it might not be available in the MSI you downloaded/installed. .. no wait, the files are there in gdal-18-1500-core.msi\GDAL.cab\, so @geographika is definitely on the right track.

As an alternative solution, the PostgreSQL OGR driver is provided with OSGeo4W. For example, from an OSGeo4W shell:

C:\OSGeo4W\bin>python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on
win32Type "help", "copyright", "credits" or "license" for more information.
>>> from osgeo import ogr
>>> print ogr.GetDriverByName("postgresql")
<osgeo.ogr.Driver; proxy of <Swig Object of type 'OGRDriverShadow *' at 0x0093A1A0> >
1
  • Thanks - I wish I could just install something like OSGeo4W but I don't have access to the internet during the setup process so I have to avoid package manager type solutions :(.
    – fod
    Commented Aug 18, 2011 at 16:50

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.