PostgREST 是一个独立的 Web 服务器,为 Postgresql 数据库生成 RESTful API。 它提供基于底层数据库结构定制的 API。
1.安装 Postgresql
(1)启动pg
(2)Docker 会暴露一个 5432 端来供你访问 Postgresql server
sudo docker run --name tutorial -p 5432:5432 \ -e POSTGRES_PASSWORD=mysecretpassword \ -d postgres
2.安装 PostgREST
(1)MACOS 下载 https://github.com/PostgREST/postgrest/releases/tag/v6.0.2 (postgrest-v6.0.2-osx.tar.xz) 解压执行。
(2)执行 ./postgrest
3. 为 API 创建数据
(1)连上容器内的 sql 控制台 (psql)
执行:sudo docker exec -it tutorial psql -U postgres。
(2)暴露在 API 中的数据库对象创建一个 命名的 schema
create schema api;
(3)我们的 API 准备通过表来设置一个端点/todos
。
create table api.todos ( |
接下来,创建一个角色来用于进行匿名的 web 请求。当一个请求进来,PostgREST 会在数据库中切换到该角色进行查询。
create role web_anon nologin; |
web_anon
角色拥有访问 api
schema 的权限,可以读取 todos
表中的数据(rows)。
4.运行 PostgRES
(1)PostgREST 使用一个配置文件来确定如何连接数据库。创建一个文件 tutorial.conf
并加上如下内容:
db-uri = "postgres://postgres:mysecretpassword@localhost/postgres" |
(2)在目录postgrest下执行
./postgrest tutorial.conf
(3)web服务启动起来
Listening on port 3000 Attempting to connect to the database... Connection successful
打开一个新的 terminal (保持 PostgREST 依旧运行)。尝试对 todos 做一个 HTTP 请求。
|
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。