5 分钟跑通 PostgREST:从一张表到第一个 PostgreSQL REST API 5 分钟跑通 PostgREST从一张表到第一个 PostgreSQL REST API【免费下载链接】postgrestREST API for any Postgres database项目地址: https://gitcode.com/GitHub_Trending/po/postgrest你手边已经有一个 PostgreSQL 库想立刻把它变成对外暴露的 REST APIPostgREST 就是干这个的把表直接映射成端点权限交给数据库角色过滤写在 URL 查询参数里一行后端代码都不用写。选对姿势装 PostgREST十分钟都不用先说结论装 PostgREST 这件事本身没有门槛三种姿势按场景选就行。macOS 上brew install postgrest一行搞定适合本地开发Linux 上pacman -S postgrest或apt install postgrest适合你不想碰容器、直接跑在机器上的场景想要环境完全一致就一行命令拉起 PostgREST 容器docker run --rm --nethost -p 3000:3000 \ -e PGRST_DB_URIpostgres://app_user:passwordlocalhost/postgres \ postgrest/postgrest姿势一条命令适合谁包管理器brew install postgrest本地开发省心官方二进制解压 release 包里的postgrest单文件要锁定具体版本Dockerdocker run -e PGRST_DB_URI... postgrest/postgrest生产、CI⚠️ 生产环境建议 Docker镜像极小环境一致坏了一眼就能看出来。最小可运行配置只写四个参数装好之后先别急着看全部配置项。让 PostgREST 活过来只需要四个参数# postgrest.conf db-uri postgres://authenticator:mysecretlocalhost:5432/postgres db-schemas api db-anon-role web_anon server-port 3000db-uri是连接串里面这个角色必须是后面会讲的认证器db-schemas告诉 PostgREST 暴露哪个 schemadb-anon-role是匿名请求用的角色。端口不写默认就是 3000连接池默认 10够起步用了。postgrest postgrest.conf看到API server listening on port 3000就说明它活了。另开一个终端验证curl http://localhost:3000/返回一个空的 JSON 对象{}没报 500链路就通了。三个角色看懂 PostgREST 安全模型理解 PostgREST 的权限记住一条链路就够了打个比方authenticator 就像前台每个进来的请求先由它接电话带着 JWT 的它转给令牌里声明的那个人没带令牌的一律转给接待处 web_anon。至于对方能碰哪些表全看数据库里怎么授权跟 PostgREST 本身无关。这就是安全交给数据库的含义。三个角色建起来其实就五行create role authenticator login noinherit password mysecret; create role web_anon nologin; create role web_user nologin; grant web_anon, web_user to authenticator;端到端实战建张 todos 表curl 把它摸一遍下面是一个完整的故事线建表、授权、起服务然后对/todos做一轮增删改查。先建表这是 API 的第一个端点create schema api; create table api.todos ( id int generated by default as identity primary key, task text not null, done boolean not null default false, due timestamptz ); insert into api.todos (task) values (跑通 PostgREST);再给匿名角色开权限grant usage on schema api to web_anon; grant select on api.todos to web_anon;配置文件换成上面那四个参数db-schemas填api。启动后开测curl http://localhost:3000/todos你应看到一行 JSON 数组跑通 PostgREST那条就在里面响应头里还有Content-Range。curl -X POST http://localhost:3000/todos \ -H Content-Type: application/json \ -d {task:再写条}你应看到201 加上新记录id 是数据库自动生成的。curl -X PATCH http://localhost:3000/todos?doneis.false \ -d {done:true}你应看到204 无内容之前那条被标记完成了。curl -X DELETE http://localhost:3000/todos?doneis.false你应看到204记录没了。顺手测一下过滤和排序这是 PostgREST 日常最好用的部分curl http://localhost:3000/todos?doneis.trueselecttask,dueorderdue.asc只回传你select里的列按due升序。要是忘了建表就请求/todos会得到 404拿只读角色去 POST会得到 401 加一句permission denied——这两条报错是这套权限模型在正常工作的证明。上生产前六项加固清单给需要按用户隔离的表启用行级安全alter table api.todos enable row level security;然后写策略。连接池按并发调db-pool 20默认 10不够就加别盲目拉满。JWT 密钥定期换容器里用jwt-secret-is-base64 true喂 base64 密钥比裸字符串好管理。跨域来源收紧server-cors-allowed-origins https://your-frontend.example别用默认的全放行。日志降档本地log-level debug生产error别把 debug 留在生产。兜底行数限制db-max-rows 1000加max-affected防手滑触发万行级的大查询。完整参数在 docs/references/configuration.rst 里按需查就行不用背。避坑速查现象 → 原因 → 解法现象原因解法Address already in use3000 端口被占配置里换server-port 3001failed to connect连接串或pg_hba.conf不匹配核对db-uri检查 PG 认证方式401permission denied角色没被授予对应表权限给 anon 角色补grant重启服务JWT 被拒jwt-secret不一致前后端用同一把密钥核对 base64 标记404db-schemas不含目标 schema修正db-schemas给角色 grant usage200 但空数组anon 角色缺 select补权限注意 schema 变更后要等缓存重载两个提醒改完数据库里的权限和结构PostgREST 要重载才生效Docker 部署时优先用PGRST_前缀的环境变量比如PGRST_DB_URI不用挂载配置文件。下一步往哪走跑通之后自然的下一步是看/路径返回的 OpenAPI 文档——你的 API 长什么样它自己会说再往后是多 schema 隔离和 RLS 的多租户进阶。完整配置参考见 docs/references/configuration.rst官方教程从 docs/tutorials/tut0.rst 开始。【免费下载链接】postgrestREST API for any Postgres database项目地址: https://gitcode.com/GitHub_Trending/po/postgrest创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考