pythonpostgispsycopg2postgis-raster

Insert GTiff-file into PostGIS table using raster2pgsql


I am trying to insert a GTiff-file into a specific PostGIS table using the raster2pgsql-command. So far I managed inserting the GTiff-file into the PostGIS database I am connected to. But this creates a new table with the file-name of the GTiff-file. I could also move the raster-data to the target table afterwards, but I suppose there is a more efficient way.

Here is an example:

import psycopg2
import os

tif_path = 'test.tif'
conn = psycopg2.connect(
    host = 'localhost',
    port = 5432,
    user = 'postgres',
    dbname = 'gisdb'
    )
curs = conn.cursor()
curs.execute("SET postgis.gdal_enabled_drivers = 'ENABLE_ALL';")
os.system('raster2pgsql "%s" > temp.sql'%tif_path)
curs.execute(open('temp.sql','r').read())

Is there a way to insert the raster-data directly into an existing table?

I know I can use -a to append the raster to an existing table and specify the column name by using -f. But there doesn't seem to be a way to specify the name of the table.


Solution

  • if you want to specify the table by yourself, your query must be like that:

    raster2pgsql -s 4326 -I -C -M C:\temp\test_1.tif -t 100x100 myschema.mytable > out.sql
    

    if you want to add the raster to existing table, you are right you must use "-a" value