[GIS] ogr2ogr in Windows cmd prompt unable to find driver ‘PostgreSQL’

ogr2ogrosgeo4wpostgresql

I can successfully execute the following command in the OSGeo4W shell:

ogr2ogr -f "PostgreSQL" -a_srs "EPSG:102733" PG:"host=localhost user='postgres' dbname='nameofdb' password='password' port=5433" "Water and Sewer Map.dxf" -nln eastover

but whenever I open up a blank Windows CMD and navigate to the folder where my file is and run the same command, I get this:

Unable to find driver `PostgreSQL'.

But when I run this in the Windows CMD:

ogr2ogr --version

I get this:

GDAL 2.0.1, released 2015/09/15

My goal is to have this as a batch file so I can run it from the native CMD prompt. Why is it saying the I don't have the PostgreSQL driver on the Windows CMD and how to I fix it?

UPDATE

Per AlecZ's recommendation, I have taken the o4w_env.bat script and appended my command to the end to look like this:

REM Make parent of this script location our current directory,
REM converting UNC path to drive letter if needed

pushd %~dp0
cd ..

REM set OSGEO4W_ROOT to short path version
set OSGEO4W_ROOT="C:\OSGeo4W64"

REM start with clean path
set path=%OSGEO4W_ROOT%\bin;%WINDIR%\system32;%WINDIR%;%WINDIR%\WBem;%OSGEO4W_ROOT%\share

for %%f in ("%OSGEO4W_ROOT%\etc\ini\*.bat") do call "%%f"

popd

cd network share:\folder\folder\folder

ogr2ogr -f "PostgreSQL" -a_srs "EPSG:102733" PG:"host=localhost user='username' dbname='databasename' password='password' port=5433" "Water and Sewer Map.dxf" -nln eastover

pause

But now I am getting this error:

ERROR 4: Unable to open EPSG support file gcs.csv.
Try setting the GDAL_DATA environment variable to point to the
directory containing EPSG csv files.
ERROR 1: Failed to process SRS definition: EPSG:102733

I have updated the environment variable as suggested in the error and added ;%OSGEO4W_ROOT%\share to the end of the clean path setting, but neither seems to be working. Any other suggestions?

picture of environment variable

Best Answer

The easy workaround is to use a .bat file you already have, that automatically sets all the dependencies you need. This is located in your "bin" folder within the main folder for your OSgeo4W installation. The name of the bat is "o4w_env.bat" - simply copy this to any location you please, change the name, and then for any command line calls you can simply add those to the bottom of the text in the bat file, in the line(s) under "popd".

One change that you'll need to make is to hard-code the directory for OSGeo4W (the bat is set to run out of that bin folder, and we're moving it). Change the following line:

for %%i in ("%CD%") do set OSGEO4W_ROOT=%%~fsi

To something like this (but changed to match the install folder on your system):

set OSGEO4W_ROOT="C:\OSGeo4W64"

When you run that, it'll set your environments correctly to include the drivers, and THEN execute your desired ogr2ogr command, as specified.

Related Question