http

HTTP客户端,允许在数据库内收发HTTP请求 (supabase)

概览

扩展包名版本分类许可证语言
pg_http1.7.1UTILMITC
ID扩展名BinLibLoadCreateTrustReloc模式
4070http-
相关扩展pg_net pg_curl pgjwt pg_smtp_client gzip bzip zstd pgjq pgmb

版本

类型仓库版本PG 大版本包名依赖
EXTMIXED1.7.11817161514pg_http-
RPMPIGSTY1.7.11817161514pgsql_http_$v-
DEBPGDG1.7.11817161514postgresql-$v-http-
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
u26.x86_64
u26.aarch64

构建

您可以使用 pig build 命令构建 pg_http 扩展的 RPM 包:

pig build pkg pg_http         # 构建 RPM 包

安装

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

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

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

pig install pg_http;          # 当前活跃 PG 版本安装
pig ext install -y pg_http -v 18  # PG 18
pig ext install -y pg_http -v 17  # PG 17
pig ext install -y pg_http -v 16  # PG 16
pig ext install -y pg_http -v 15  # PG 15
pig ext install -y pg_http -v 14  # PG 14
dnf install -y pgsql_http_18       # PG 18
dnf install -y pgsql_http_17       # PG 17
dnf install -y pgsql_http_16       # PG 16
dnf install -y pgsql_http_15       # PG 15
dnf install -y pgsql_http_14       # PG 14
apt install -y postgresql-18-http   # PG 18
apt install -y postgresql-17-http   # PG 17
apt install -y postgresql-16-http   # PG 16
apt install -y postgresql-15-http   # PG 15
apt install -y postgresql-14-http   # PG 14

创建扩展

CREATE EXTENSION http;

用法

来源:READMEv1.7.1 release

http 允许 SQL 代码通过 libcurl 发起 HTTP 请求。它适合受控的集成点,例如触发器通知外部服务、SQL job 拉取小型远程 payload,或数据库侧 webhook 调用。

CREATE EXTENSION http;

请求与响应类型

每个请求使用 http_request,并返回 http_response

http_request(method http_method, uri varchar, headers http_header[], content_type varchar, content varchar)
http_response(status integer, content_type varchar, headers http_header[], content varchar)

便捷包装函数最终调用同一个底层 http(http_request) 函数:

  • http_get(uri varchar)
  • http_get(uri varchar, data jsonb)
  • http_post(uri varchar, content varchar, content_type varchar)
  • http_post(uri varchar, data jsonb)
  • http_put(uri varchar, content varchar, content_type varchar)
  • http_patch(uri varchar, content varchar, content_type varchar)
  • http_delete(uri varchar)
  • http_head(uri varchar)

示例

SELECT status, content_type, content
FROM http_get('https://httpbun.com/ip');

SELECT content::json->'headers'->>'Authorization'
FROM http((
  'GET',
  'https://httpbun.com/headers',
  http_headers('Authorization', 'Bearer token'),
  NULL,
  NULL
)::http_request);

SELECT status, content::json->'form' AS form
FROM http_post(
  'https://httpbun.com/post',
  jsonb_build_object('myvar', 'myval', 'foo', 'bar')
);

SELECT status, content_type, content::json->>'data' AS data
FROM http_put('https://httpbun.com/put', 'some text', 'text/plain');

通过展开 headers 数组检查响应头:

SELECT (unnest(headers)).*
FROM http_get('https://httpbun.com/');

二进制内容

README 提醒,varchar::bytea 对二进制响应体不安全,因为它会在零值字节处停止。读取响应内容时使用 text_to_bytea(content),发送二进制请求体时使用 bytea_to_text(bytea)

WITH http AS (
  SELECT * FROM http_get('https://httpbingo.org/image/png')
)
SELECT content_type, length(text_to_bytea(content)) AS bytes
FROM http;

超时与版本说明

pg_http 1.7.1 是兼容性和文档版本:增加 timeout examples,增加 PostgreSQL 17 wait-event hooks,并包含 PostgreSQL 19 support fixes。用户可见 SQL API 仍是上面的 README surface。


最后修改 2026-07-02: extension update 2026-07-02 (d4da20c)