I have a fairly simple script where I aim to insert roughly 30 raster TIFFs into a PostgreSQL database.

To manually import one TIFF at a time I run this command

raster2pgsql.exe -s 2263 -d -C -I -M -l 2,4,8,16 \\rasterpath\_001.tif -F -t 100x100 dem_001 | psql -d raster_test -U postgres -p 5434

This works great

Now to the Python script

for x in os.listdir(folder):
    if x.endswith('.tif'):
        pg= "raster2pgsql.exe -s 2263 -d -C -I -M -l 2,4,8,16 {0} -F -t 100x100 {1} | psql -d raster_test -U postgres -p 5434 ".format(os.path.join(folder,x),fname)

I get this error

ERROR: Unable to read raster file: dem_001
ERROR: Unable to read raster file: dem_002

But if I run the printed output from the Python script manually into the CMD it succesfully runs.

Kind of confused on what is going wrong here.

Using Python 3.7

  • 1
    Maybe your Python script is executing the commandline from a different folder as where your raster files are ... You can run echo %cd% from within your Python script to find out from which directory you are actually executing ... Nov 7 '19 at 21:04
  • This might be easier with a batch script: bash: "for fname in dem_001 dem_002;do cmd;done" dos: "FOR %%A IN (list) DO command parameters"
    – vinh
    Nov 8 '19 at 16:38
  • @vinh any docs/examples with this method?
    – ziggy
    Nov 8 '19 at 17:34

In response to comment, something like this would work in bash:

OPTIONS="-s 2263 -d -C -I -M -l 2,4,8,16 -F -t 100x100" 
CMD_PIPE="psql -d raster_test -U postgres -p 5434"
for fname in dem_001 dem_002;
do raster2pgsql $OPTIONS *.tif $fname | $CMD_PIPE;

:: windows (not tested)
:: FOR %%A IN (list) DO command parameters
%%OPTIONS="-s 2263 -d -C -I -M -l 2,4,8,16 -F -t 100x100"
FOR %%fname IN (dem_001 dem_002)
DO (raster2pgsql.exe %%OPTIONS *.tif %%fname >> %%TMP_FILE)
  • using windows :(
    – ziggy
    Nov 12 '19 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.