I'd like to create a PostGIS raster using GDAL and python.

I can get the driver easily:

drv = gdal.GetDriverByName("PostGISRaster")

But how can I create a new raster table? I can't find any information about drv.Create to explain what to put there....?

2 Answers 2


It looks like the ability to create rasters is not yet available in the driver. However, you can create a layer using your standard PostgreSQL Python driver (i.e. psycopg2) and then open it from GDAL.

So you'd create a layer using ST_MakeEmptyRaster and ST_AddBand:

CREATE TABLE rtest (gid serial primary key, rast raster);
INSERT INTO rtest (rast) VALUES (ST_AddBand(ST_MakeEmptyRaster(100, 100, 1, 1, 2), '8BUI'::text));

Then you'd open and manipulate the raster in GDAL/Python

ds = gdal.Open('PG:host=localhost dbname=mydb table=rtest user=myuser')

I haven't tested this extensively yet, but it seems to work. doesn't work at all. I get the error,

Writing through PostGIS Raster band not supported yet

when I try to write.

You could use Python and psycopg2 to write chunks of your image to a raster table using ST_SetValues.

  • yeah...but you wouldn't be able to write pixel values to the raster (without explicitly using postgis functions), right?
    – James
    Jul 31, 2015 at 10:38
  • Hah, you're right. I didn't test that one crucial step. Made a small edit to my answer to reflect that.
    – Rob Skelly
    Jul 31, 2015 at 15:57

Recently came across this and it ended up helping me, so I thought I would share an update.

Adding additional arguments help as well as adding raster constraints to the table you are trying to use.

ds = gdal.Open('PG:host='' dbname='' schema='' table='' user='' port='' password='' column='' mode=2')

SELECT AddRasterConstraints('raster_table'::name, 'column_name'::name);

Hope this helps someone else too with the added details.

Your Answer

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

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