Monday, 9 April 2018

GDAL Python bindings not picking up OGR PostgreSQL driver


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?



Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...