http
HTTP客户端,允许在数据库内收发HTTP请求 (supabase)
仓库
pramsey/pgsql-http
https://github.com/pramsey/pgsql-http
源码
pgsql-http-1.7.1.tar.gz
pgsql-http-1.7.1.tar.gz
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
pg_http | 1.7.2 | UTIL | MIT | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 4070 | http | 否 | 是 | 否 | 是 | 否 | 否 | - |
| 相关扩展 | pg_net pg_curl pgjwt pg_smtp_client gzip bzip zstd pgjq pgmb |
|---|---|
| 下游依赖 | pgmb |
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PGDG | 1.7.2 | 1817161514 | pg_http | - |
| RPM | PGDG | 1.7.2 | 1817161514 | pgsql_http_$v | - |
| DEB | PGDG | 1.7.2 | 1817161514 | postgresql-$v-http | - |
构建
您可以使用 pig build 命令构建 pg_http 扩展的 RPM 包:
pig build pkg pg_http # 构建 RPM 包
安装
您可以直接安装 pg_http 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:
pig repo add pgdg -u # 添加 PGDG 仓库并更新缓存
使用 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;
用法
来源:
http 允许 PostgreSQL 通过 libcurl 发出同步 HTTP 请求。它适用于受控集成和管理调用,但后端会在 SQL 语句和事务内部等待远程服务的响应。限制可以调用它的用户、设置较短的超时时间,并确保不可信输入不会选择任意 URL。
核心工作流程
CREATE EXTENSION http;
SELECT status, content_type, content
FROM http_get('https://httpbingo.org/get');
发送 JSON 并检查响应:
SELECT status, content::jsonb
FROM http_post(
'https://httpbingo.org/post',
'{"event":"invoice.paid"}',
'application/json'
);
通用入口点接受完整的请求:
SELECT (http((
'GET',
'https://httpbingo.org/headers',
http_headers('Authorization', 'Bearer example'),
NULL,
NULL
)::http_request)).status;
重要对象
http_request包含method,uri,headers,content_type, 和content。http_response包含status,content_type,headers, 和content。http_header,http_header(...), 和http_headers(...)构建请求头;unnest(response.headers)将响应头暴露为行。http(...)执行完整的http_request。http_get,http_post,http_put,http_patch,http_delete, 和http_head是方便的包装器。urlencode(text)和urlencode(jsonb)编码查询数据。http_set_curlopt,http_list_curlopt, 和http_reset_curlopt管理支持的会话级 libcurl 设置。
超时、连接和安全性
默认情况下,每个请求使用一个新的连接。在测量后端生命周期和远程服务器行为之后再启用持久连接:
SET http.curlopt_timeout_ms = 1000;
SET http.curlopt_connecttimeout_ms = 250;
SET http.curlopt_tcp_keepalive = 1;
默认请求超时为五秒。超时将引发 SQL 错误,因此调用者必须处理事务回滚。触发器或长时间事务中的网络延迟可能会持有锁并耗尽数据库连接;优先使用外部工作进程进行持久异步传递。
保持 TLS 验证启用、保护包含凭据的 curl 设置、在使用前验证响应状态和内容,并在网络层和 SQL 权限层限制出站目的地。版本 1.7.2 包含相对于 1.7.1 的构建、测试和 curl 选项常量维护;它没有引入实质性 SQL API 变更。控制文件仍然声明 SQL 扩展版本为 1.7。