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:
- Using raster2pgsql in Python console of QGIS?
- Import raster into PostGIS using Python
- Upload a raster in PostGIS
- Unable to read TIFF file with raster2pgsql
but did not come up with the solution
How to rectify this?
asked May 29, 2021 at 14:20
lang-sql
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.