postgis

PostGIS 几何和地理空间扩展

概览

扩展包名版本分类许可证语言
postgis3.6.3GISGPL-2.0C
ID扩展名BinLibLoadCreateTrustReloc模式
1500postgis-
1501postgis_topologytopology
1502postgis_raster-
1503postgis_sfcgal-
1504postgis_tiger_geocodertiger
1505address_standardizer-
1506address_standardizer_data_us-
相关扩展pointcloud h3 pg_geohash geoip pg_polyline earthdistance ogr_fdw tzf
下游依赖documentdb h3_postgis mobilitydb pgrouting pointcloud_postgis postgis_raster postgis_sfcgal postgis_tiger_geocoder postgis_topology pg_eviltransform

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG3.6.31817161514postgis-
RPMPGDG3.6.31817161514postgis36_$v-
DEBPGDG3.6.31817161514postgresql-$v-postgis-3-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64

安装

您可以直接安装 postgis 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:

pig repo add pgdg -u          # 添加 PGDG 仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install postgis;          # 当前活跃 PG 版本安装
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 postgis;

用法

来源:Official manualcurrent manual HTMLrelease notespatch release announcement

postgis 为 PostgreSQL 增加空间类型、索引与 SQL 函数。对用户来说,最主要的区分是:geometry 用于平面或投影坐标工作,geography 用于经纬度数据上的球面计算。

基本设置

CREATE EXTENSION postgis;
SELECT PostGIS_Full_Version();

核心类型与函数

CREATE TABLE sensors (
  id bigserial PRIMARY KEY,
  geom geometry(Point, 4326),
  geog geography(Point, 4326)
);

SELECT ST_SetSRID(ST_MakePoint(-73.985, 40.748), 4326);
SELECT ST_Intersects(a.geom, b.geom) FROM a, b;
SELECT ST_DWithin(a.geom, b.geom, 100);
SELECT ST_Distance(a.geog, b.geog);
SELECT ST_Transform(geom, 3857) FROM sensors;
  • 构造函数:ST_MakePointST_GeomFromTextST_GeomFromGeoJSON
  • 空间关系:ST_IntersectsST_ContainsST_WithinST_DWithin
  • 度量与坐标变换:ST_DistanceST_AreaST_LengthST_Transform
  • 几何处理:ST_BufferST_IntersectionST_Union

空间索引

CREATE INDEX idx_sensors_geom ON sensors USING GIST (geom);

官方手册仍将 GiST 作为通用空间索引的首选建议;对于特定数据分布与权衡,也可以考虑 BRIN 与 SP-GiST。

注意事项

  • 进行平面距离与面积计算时,应在合适的投影 SRID 中使用 geometry;需要以米为单位的球面计算时,应使用 geography
  • PostGIS 3.6.3 是一个发布日期为 2026-04-14 的补丁版本。release notes 描述的是修复项与一项安全加固变更,而不是新的 stub 级使用接口,因此这次刷新主要是裁剪并对齐当前手册。

最后修改 2026-04-19: update extension stub docs (aa5941a)