ogr_fdw

GIS 数据外部数据源包装器

概览

扩展包名版本分类许可证语言
ogr_fdw1.1.7GISMITC
ID扩展名BinLibLoadCreateTrustReloc模式
1550ogr_fdw-
相关扩展postgis file_fdw postgres_fdw postgis_topology postgis_raster postgis_sfcgal postgis_tiger_geocoder address_standardizer

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG1.1.71817161514ogr_fdw-
RPMPGDG1.1.71817161514ogr_fdw_$v-
DEBPGDG1.1.71817161514postgresql-$v-ogr-fdw-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
d12.aarch64
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
d13.x86_64
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
d13.aarch64
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
u22.x86_64
u22.aarch64
PGDG 1.1.7
u24.x86_64
u24.aarch64
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7
PGDG 1.1.7

安装

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

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

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

pig install ogr_fdw;          # 当前活跃 PG 版本安装
pig ext install -y ogr_fdw -v 18  # PG 18
pig ext install -y ogr_fdw -v 17  # PG 17
pig ext install -y ogr_fdw -v 16  # PG 16
pig ext install -y ogr_fdw -v 15  # PG 15
pig ext install -y ogr_fdw -v 14  # PG 14
dnf install -y ogr_fdw_18       # PG 18
dnf install -y ogr_fdw_17       # PG 17
dnf install -y ogr_fdw_16       # PG 16
dnf install -y ogr_fdw_15       # PG 15
dnf install -y ogr_fdw_14       # PG 14
apt install -y postgresql-18-ogr-fdw   # PG 18
apt install -y postgresql-17-ogr-fdw   # PG 17
apt install -y postgresql-16-ogr-fdw   # PG 16
apt install -y postgresql-15-ogr-fdw   # PG 15
apt install -y postgresql-14-ogr-fdw   # PG 14

创建扩展

CREATE EXTENSION ogr_fdw;

用法

ogr_fdw: PostgreSQL 的 OGR 外部数据包装器

OGR 是 GDAL 空间数据访问库的矢量部分。它允许通过简单的 C API 访问大量 GIS 数据格式。由于 OGR 暴露了简单的表结构,而 PostgreSQL 外部数据包装器允许访问表结构,两者的契合非常完美。

快速开始

CREATE EXTENSION postgis;
CREATE EXTENSION ogr_fdw;

使用 ogr_fdw_info 工具读取 OGR 数据源并输出服务器/表定义:

ogr_fdw_info -s /tmp/test -l pt_two
CREATE SERVER "myserver"
  FOREIGN DATA WRAPPER ogr_fdw
  OPTIONS (
    datasource '/tmp/test',
    format 'ESRI Shapefile' );

CREATE FOREIGN TABLE "pt_two" (
  fid integer,
  "geom" geometry(Point, 4326),
  "name" varchar,
  "age" integer,
  "height" real,
  "birthdate" date )
  SERVER "myserver"
  OPTIONS (layer 'pt_two');

SELECT * FROM pt_two;

支持过滤下推——包括简单谓词和边界框过滤(&&):

SET client_min_messages = debug1;

SELECT name, age, height
FROM pt_two
WHERE height < 5.7
AND geom && ST_MakeEnvelope(0, 0, 1, 1);
DEBUG:  OGR SQL: (height < 5.7)
DEBUG:  OGR spatial filter (0 0, 1 1)

限制

  • 需要 PostgreSQL 11 或更高版本
  • 仅有限的非空间查询限制会下推到 OGR(仅 ><<=>==
  • 仅边界框过滤(&&)会下推到空间过滤
  • OGR 连接每次查询都会创建(无连接池)
  • 每次都会检索所有列

示例

WFS(Web 要素服务)

CREATE SERVER geoserver
  FOREIGN DATA WRAPPER ogr_fdw
  OPTIONS (
    datasource 'WFS:https://demo.geo-solutions.it/geoserver/wfs',
    format 'WFS' );

CREATE FOREIGN TABLE topp_states (
  fid bigint,
  the_geom Geometry(MultiSurface,4326),
  gml_id varchar,
  state_name varchar,
  state_fips varchar,
  state_abbr varchar,
  land_km double precision,
  persons double precision )
  SERVER "geoserver"
  OPTIONS (layer 'topp:states');

文件地理数据库

CREATE SERVER fgdbtest
  FOREIGN DATA WRAPPER ogr_fdw
  OPTIONS (
    datasource '/tmp/Querying.gdb',
    format 'OpenFileGDB' );

CREATE FOREIGN TABLE cities (
  fid integer,
  geom geometry(Point, 4326),
  city_name varchar,
  state_name varchar,
  elevation integer,
  pop1990 integer )
  SERVER fgdbtest
  OPTIONS (layer 'Cities');

高级功能

可写表

如果 OGR 驱动支持,你可以插入/更新/删除记录。可写表需要在表定义中包含 fid 列。

ALTER SERVER myserver
  OPTIONS (ADD updateable 'true');

列名映射

将远程列名映射到本地列名:

CREATE FOREIGN TABLE typetest_fdw_mapped (
  fid bigint,
  supertime time OPTIONS (column_name 'clock'),
  thebestname varchar OPTIONS (column_name 'name') )
  SERVER wraparound
  OPTIONS (layer 'typetest');

自动表导入

使用 IMPORT FOREIGN SCHEMA 自动创建外部表定义:

CREATE SCHEMA fgdball;

-- 导入所有表
IMPORT FOREIGN SCHEMA ogr_all
  FROM SERVER fgdbtest
  INTO fgdball;

-- 导入指定表
IMPORT FOREIGN SCHEMA ogr_all
  LIMIT TO(cities)
  FROM SERVER fgdbtest
  INTO fgdball;

GDAL 选项

通过配置和打开选项控制驱动行为:

CREATE SERVER myserver_latin1
  FOREIGN DATA WRAPPER ogr_fdw
  OPTIONS (
    datasource '/tmp/test',
    format 'ESRI Shapefile',
    config_options 'SHAPE_ENCODING=LATIN1' );

多个配置选项可以作为空格分隔的列表传递。


最后修改 2026-03-14: update extension metadata (953cbd0)