postgis_raster
PostGIS raster types and functions
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
postgis | 3.6.2 | GIS | GPL-2.0 | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 1500 | postgis | No | Yes | No | Yes | No | No | - |
| 1501 | postgis_topology | No | Yes | No | Yes | No | No | topology |
| 1502 | postgis_raster | No | Yes | No | Yes | No | No | - |
| 1503 | postgis_sfcgal | No | Yes | No | Yes | No | Yes | - |
| 1504 | postgis_tiger_geocoder | No | Yes | No | Yes | Yes | No | tiger |
| 1505 | address_standardizer | No | Yes | No | Yes | No | Yes | - |
| 1506 | address_standardizer_data_us | No | Yes | No | Yes | No | Yes | - |
| Related | postgis pointcloud_postgis pointcloud pgrouting h3 q3c ogr_fdw geoip pg_polyline |
|---|---|
| Depended By | h3_postgis |
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PGDG | 3.6.2 | 1817161514 | postgis | postgis |
| RPM | PGDG | 3.6.2 | 1817161514 | postgis36_$v | - |
| DEB | PGDG | 3.6.2 | 1817161514 | postgresql-$v-postgis-3 | - |
| OS / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PGDG 3.6.1 | PGDG 3.6.1 | PGDG 3.6.1 | PGDG 3.6.1 | PGDG 3.6.1 |
| el8.aarch64 | PGDG 3.6.1 | PGDG 3.6.1 | PGDG 3.6.1 | PGDG 3.6.1 | PGDG 3.6.1 |
| el9.x86_64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| el9.aarch64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| el10.x86_64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| el10.aarch64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| d12.x86_64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| d12.aarch64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| d13.x86_64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| d13.aarch64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| u22.x86_64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| u22.aarch64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| u24.x86_64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
| u24.aarch64 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 | PGDG 3.6.2 |
Install
You can install postgis directly. First, make sure the PGDG repository is added and enabled:
pig repo add pgdg -u # Add PGDG repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install postgis; # Install for current active PG version
pig ext install -y postgis -v 18 # PG 18
pig ext install -y postgis -v 17 # PG 17
pig ext install -y postgis -v 16 # PG 16
pig ext install -y postgis -v 15 # PG 15
pig ext install -y postgis -v 14 # PG 14
dnf install -y postgis36_18 # PG 18
dnf install -y postgis36_17 # PG 17
dnf install -y postgis36_16 # PG 16
dnf install -y postgis36_15 # PG 15
dnf install -y postgis36_14 # PG 14
apt install -y postgresql-18-postgis-3 # PG 18
apt install -y postgresql-17-postgis-3 # PG 17
apt install -y postgresql-16-postgis-3 # PG 16
apt install -y postgresql-15-postgis-3 # PG 15
apt install -y postgresql-14-postgis-3 # PG 14
Create Extension:
CREATE EXTENSION postgis_raster CASCADE; -- requires: postgis
Usage
PostGIS Raster extends PostGIS with support for raster (gridded) data stored directly in PostgreSQL. It enables raster analysis, raster/vector interaction, and map algebra operations within SQL.
Setup
CREATE EXTENSION postgis_raster;
Loading Raster Data
The raster2pgsql command-line tool imports raster files (GeoTIFF, etc.) into PostgreSQL:
# Load a GeoTIFF as tiled 100x100 rasters, create spatial index, use COPY
raster2pgsql -s 4326 -t 100x100 -I -C -M elevation.tif public.dem | psql mydb
# Append to existing table
raster2pgsql -s 4326 -t 100x100 -a more_data.tif public.dem | psql mydb
Key flags:
-s <srid>– Set the SRID-t <width>x<height>– Tile the raster into chunks-I– Create a spatial GiST index-C– Apply raster constraints-M– Vacuum analyze after loading
Querying Raster Data
Raster Metadata
-- Get raster dimensions and pixel size
SELECT rid,
ST_Width(rast) AS width,
ST_Height(rast) AS height,
ST_ScaleX(rast) AS pixel_size_x,
ST_ScaleY(rast) AS pixel_size_y,
ST_NumBands(rast) AS bands,
ST_SRID(rast) AS srid
FROM dem LIMIT 5;
Pixel Values
-- Get the value at a specific point
SELECT ST_Value(rast, ST_SetSRID(ST_MakePoint(-73.985, 40.748), 4326)) AS elevation
FROM dem
WHERE ST_Intersects(rast, ST_SetSRID(ST_MakePoint(-73.985, 40.748), 4326));
-- Get value at column/row position (band 1)
SELECT ST_Value(rast, 1, 10, 20) FROM dem WHERE rid = 1;
Band Statistics
SELECT (ST_SummaryStats(rast)).*
FROM dem WHERE rid = 1;
-- Returns: count, sum, mean, stddev, min, max
Raster Processing
Clipping Rasters by Vector Geometry
-- Clip raster to a polygon boundary
SELECT ST_Clip(rast, geom) AS clipped_rast
FROM dem, boundaries
WHERE ST_Intersects(rast, geom);
Map Algebra
Apply pixel-by-pixel operations:
-- Single-raster map algebra: classify elevation
SELECT ST_MapAlgebra(rast, 1, NULL,
'CASE WHEN [rast] > 100 THEN 1 WHEN [rast] > 50 THEN 2 ELSE 3 END') AS classified
FROM dem;
-- Two-raster map algebra: difference between two DEMs
SELECT ST_MapAlgebra(a.rast, 1, b.rast, 1, '[rast1] - [rast2]') AS diff
FROM dem_old a, dem_new b
WHERE ST_Intersects(a.rast, b.rast);
Raster/Vector Interaction
-- Convert raster pixels to vector points
SELECT (ST_PixelAsPoints(rast)).*
FROM dem WHERE rid = 1;
-- Convert raster to polygons (one per unique value)
SELECT (ST_DumpAsPolygons(rast)).*
FROM dem WHERE rid = 1;
-- Intersect raster with vector and get values
SELECT p.name, ST_Value(d.rast, p.geom) AS elevation
FROM dem d, points p
WHERE ST_Intersects(d.rast, p.geom);
Resampling and Reprojection
-- Resample to a different pixel size
SELECT ST_Rescale(rast, 0.001, -0.001) FROM dem;
-- Reproject to a different SRID
SELECT ST_Transform(rast, 3857) FROM dem;
Exporting Rasters
-- Export as GeoTIFF (binary)
SELECT ST_AsTIFF(rast) FROM dem WHERE rid = 1;
-- Export as PNG
SELECT ST_AsPNG(rast) FROM dem WHERE rid = 1;
-- Export as JPEG
SELECT ST_AsJPEG(rast) FROM dem WHERE rid = 1;
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.