0

Tried many ways

A.

import os
import subprocess
from subprocess import PIPE, run

db_name = 'agro_dss'
db_host = 'localhost'
db_port = '5432'
db_user = 'postgres'
db_password = 'imd123#'


os.environ['PATH'] = r';C:\Program Files\PostgreSQL\13\bin'
os.environ['PGHOST'] = db_host
os.environ['PGPORT'] = db_port
os.environ['PGUSER'] = db_user
os.environ['PGPASSWORD'] = db_password
os.environ['PGDATABASE'] = db_name


cmd = 'raster2pgsql -s 4326 -C -t auto -M D:\Advisory\Raster\*.tif -F public.polys | psql -U {} -d {} -h {} -p {}'.format(db_user,db_name,db_host,db_port)


subprocess.call(cmd, shell=True)
result = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)
print(result.returncode, result.stdout, result.stderr)

The error is

1  ERROR: Unable to read raster file: public.polys

B.

from osgeo import gdal, osr
import psycopg2
import subprocess
import sys, os

fileName = r"D:\Advisory\Raster\MODIS_All_India_NDVI_2001_01_01.tif"
check_proj = gdal.Open(fileName)
proj = osr.SpatialReference(wkt=check_proj.GetProjection())
projection=str(proj.GetAttrValue('AUTHORITY',1))

input_path = r"D:\Advisory\Raster\\"

os.environ['PATH'] = r';C:\Program Files\PostgreSQL\13\bin'
os.environ['PGHOST'] = 'localhost'
os.environ['PGPORT'] = '5432'
os.environ['PGUSER'] = 'postgres'
os.environ['PGPASSWORD'] = 'imd123#'
os.environ['PGDATABASE'] = 'agro_dss'

for raster in os.listdir(input_path):
    if raster.endswith(".tif"):
       name = raster.split(".tif")[0]    
       raster = os.path.join(input_path, raster)                    
       rastername = str(name)
       rasterlayer = rastername.lower()
       conn = psycopg2.connect(database="agro_dss", user="postgres", host="localhost", password="imd123#", port=5432)
       cursor = conn.cursor()
       print(cursor," connection suscessfull")
       cmds = 'raster2pgsql -s '+projection+' -I -C -M "'+raster+'" -F -t auto'+' public.'+name+' | psql -d agro_dss -U postgres -p 5432 -h localhost'
       subprocess.call(cmds, shell=True)
       result = run(cmds, stdout=PIPE, stderr=PIPE, universal_newlines=True)
       print(result.returncode, result.stdout, result.stderr)

The error is

1  ERROR: Unable to read raster file: public.polys

Sought many answers:

but did not come up with the solution

How to rectify this?

7
  • 1
    Never, never use the postgres login for anything other than creating new user-privilege logins and roles for managing access to the server. Please ask only one question per Question.
    – Vince
    May 29 at 14:43
  • Thanks for the concern @Vince, It was on localhost, not on server though. May 29 at 14:45
  • 1
    The database instance is called a "server" even it runs on a toaster-oven. Worst-practice security destroys servers.
    – Vince
    May 29 at 14:49
  • @Vince, It has one question only. I have tried many ways to insert the raster in PostgreSQL database,but the same error is coming from all of these. May 29 at 15:51
  • 1
    If the error says it can't read "file" tablename, then it is not a schema issue (instead, it is likely a syntax issue). You have provided two different failure cases, and they are obfuscated by being invoked from Python (without even a diagnostic print statement showing the actual usage).
    – Vince
    May 29 at 17:11

Your Answer

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

Browse other questions tagged or ask your own question.