0

I am trying to export a PostGIS table to GeoJSON using ogr2ogr.

The following works perfectly when I launch it in Windows command :

ogr2ogr -f "GeoJSON" F:\output.json PG:"host=**** port=**** dbname=**** user=**** password=****" --optfile F:\query.sql`

However, when I save this command as a .bat file and then try to launch it in Windows command, I receive an error :

FAILURE: Unable to open datasource `PG:host=**** port=**** dbname=**** user=**** password=****' with the following drivers.

Is there any way I can make this command work as a .bat file?

6
  • first thing to test is using ogrinfo to check if you can connect to the database
    – Ian Turton
    Commented Oct 9, 2020 at 13:49
  • I tested the connection using ogrinfo and there is no problem. It's only when I launch this command as a .bat file that the problem occurs.
    – daustin
    Commented Oct 9, 2020 at 13:58
  • Works for me also from a batch file. Add ogrinfo PG:"host=**** port=**** dbname=**** user=**** password=****" --debug on as the first line into your batch file. Perhaps it prints some useful info for you. Do you have any odd characters that may have a special meaning for the batch file in your connection parameters?
    – user30184
    Commented Oct 9, 2020 at 14:53
  • @Ian, @user30184, I added ogrinfo PG:"host=**** port=**** dbname=**** user=**** password=****" --debug on to the batch file and I got the FAILURE: Unable to open datasource message. However, I also wrote this same command directly in Windows command and it correctly listed all tables in my database. I have not changed the connection parameters, but I am using a French computer (could language parameters possibly change how ogr2ogr deals with a batch command rather than a direct command?)
    – daustin
    Commented Oct 10, 2020 at 6:02
  • most likely problem is that there is a "special" character in your password or user name that windows doesn't like.
    – Ian Turton
    Commented Oct 10, 2020 at 8:37

1 Answer 1

2

I had the same problem with connection to oracle. This solve my problem (path to drivers):

SET GDAL_DRIVER_PATH=c:\OSGeo4W\apps\gdal\lib\gdalplugins

You should add it in your .bat file. Path depends how you installed GDAL/OGR, in my case it is OSGEO4W installation.

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.