PostgREST 快速上手:5 分钟把 PostgreSQL 表变成 REST 接口
发布时间:2026/9/4 23:43:40 锦皓数字建站

PostgREST 快速上手5 分钟把 PostgreSQL 表变成 REST 接口【免费下载链接】postgrestREST API for any Postgres database项目地址: https://gitcode.com/GitHub_Trending/po/postgrestPostgREST 是一个独立运行的 Web 服务零配置地把任何 PostgreSQL 数据库表变成标准 RESTful API适合不想手写路由、分页和权限代码的开发者。它的最大价值点是接口结构、数据校验、权限边界全部由数据库对象声明式决定应用层几乎不用写代码。从零到跑通开始前只需要三样东西PostgREST 二进制brew install postgrest、pacman -S postgrest任选其一Linux 也可下载预编译静态二进制PostgreSQL ≥ 14一个能curl的终端验证安装并查看版本postgrest --version用 Docker 起一个本地 PostgreSQL已有实例可跳过docker run --name pg -p 5432:5432 \ -e POSTGRES_PASSWORDnotused -d postgres:16建一张待办表和最小权限角色PostgREST 没有独立建模步骤表结构就是 API 结构docker exec -it pg psql -U postgres SQL create schema api; create table api.todos ( id int primary key generated by default as identity, task text not null, done boolean not null default false ); insert into api.todos (task) values (hello postgrest); create role web_anon nologin; -- 匿名请求切换到的角色 grant usage on schema api to web_anon; grant select, insert on api.todos to web_anon; create role authenticator noinherit login password mysecretpassword; -- PostgREST 登录用 grant web_anon to authenticator; -- 允许切换身份 SQL写入最小配置文件四行即够jwt-secret不配则匿名请求直接放行db-uri postgres://authenticator:mysecretpasswordlocalhost:5432/postgres db-schemas api db-anon-role web_anon启动服务并发送第一个查询postgrest ./postgrest.conf curl -s http://localhost:3000/todos返回[{id:1,task:hello postgrest,done:false}]就算跑通了 ✅。匿名角色此时只有 SELECT 权限若用 POST 向同一端点写入会收到 401这正是我们想要的效果。它到底怎么工作的一句话PostgREST 把数据库对象当路由表——表、视图、函数各变成一个端点GET/POST/PATCH/DELETE 对应查、插、改、删。白话版请求流HTTP 请求进来 → 有 JWT 就解码出 role 声明没有就用匿名角色 → 从连接池取连接、切换数据库角色 → 按 schema cache 生成参数化 SQL → 执行并返回 JSON。所以权限控制完全发生在数据库里想加一个接口建表加授权即可服务端一行代码都不用改。把一张表变成只读接口看个贴近业务的场景给社区看板做只读接口可筛选、可分页但禁止匿名写入。建表和种子数据create table api.feed ( id bigserial primary key, title text not null, author text, created_at timestamptz not null default now() ); insert into api.feed (title, author) values (PostgREST 入门, a), (连接池调优, b), (行级安全策略, c), (JWT 排错笔记, d);配置最小权限角色web_anon对feed只授予 SELECTAPI 层就不需要任何鉴权代码。复用启动步骤的命令后第一个查询长这样curl -s http://localhost:3000/feed?titlelike.*PostgREST* # [{id:1,title:PostgREST 入门,author:a,...}]查询串直接映射为 SQLselectid,author投影列ordercreated_at.desc排序limit2offset2分页or(author.eq.a,author.eq.b)表达多条件。给写操作签发 JWT把上方配置文件加上jwt-secret 至少三十二个字符的密钥字符串再重启客户端即可通过Authorization: Bearer token携带身份。PostgREST 从 token 的role声明解析出数据库角色并切换过去。授予写权限后用 token 写入create role feed_user nologin; grant usage on schema api to feed_user; grant select, insert on api.feed to feed_user;签发一个role为feed_user的 tokenHS256载荷{role:feed_user}然后curl -s -X POST http://localhost:3000/feed \ -H Authorization: Bearer $TOKEN \ -H Content-Type: application/json \ -d {title:带着 JWT 写入,author:me} # 201 Createdbody 返回新行同一命令去掉 Authorization 头重放会得到 401——同一张表权限随角色不同而不同。生产化要改的几处参数默认值生产建议为什么改jwt-secret未设置拒绝一切认证请求≥32 字符建议用文件secrets/jwt加载启用登录的前提弱密钥等于没有认证db-max-rows无上限如 1000防止恶意请求拖走全表、打爆内存server-cors-allowed-origins空 接受任意域名填白名单默认对所有前端开放跨域生产必须收紧log-levelerrorinfo每个请求落日志方便排查与审计改完不用重启killall -SIGUSR2 postgrest即可热加载数据库里执行NOTIFY pgrst, reload config效果相同。连接池db-pool默认 10先按并发估算压测后再调。下一步 资源想在函数里做行级鉴权或自定义校验从 docs/references/api/functions.rst 的 RPC 机制入手给团队生成交互式接口文档服务根路径自带 OpenAPI 输出相关行为见 docs/references/api/openapi.rst看生产观测指标连接池、schema cachedocs/references/observability.rst源码入口src/library/PostgREST/请求解析、查询规划、SQL 生成分别在 ApiRequest、Plan、Query 模块里【免费下载链接】postgrestREST API for any Postgres database项目地址: https://gitcode.com/GitHub_Trending/po/postgrest创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
锦
锦皓数字建站
深耕本土企业品牌数字化升级,专注原创端正雅致商务官网,从视觉设计到稳定运维全程保驾护航。