1

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\PostgreSQL13円\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\PostgreSQL13円\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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 29, 2021 at 14:20
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. Commented May 29, 2021 at 14:43
  • Thanks for the concern @Vince, It was on localhost, not on server though. Commented May 29, 2021 at 14:45
  • 1
    The database instance is called a "server" even it runs on a toaster-oven. Worst-practice security destroys servers. Commented May 29, 2021 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. Commented May 29, 2021 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). Commented May 29, 2021 at 17:11

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.