<span class="k"> create <span class="k">schema <span class="n">api<span class="p">;<br /> (3)我们的 API 准备通过表来设置一个端点 <code><span class="pre">/todos</span></code>。<br /><br /></span></span></span></span>
create table api.todos (<br /> id serial primary key,<br /> done boolean not null default false,<br /> task text not null,<br /> due timestamptz<br />);<br /><br />insert into api.todos (task) values<br /> (‘finish tutorial 0‘), (‘pat self on back‘); |
接下来,创建一个角色来用于进行匿名的 web 请求。当一个请求进来,PostgREST 会在数据库中切换到该角色进行查询。
create role web_anon nologin;<br />grant web_anon to postgres;<br /><br />grant usage on schema api to web_anon;<br />grant select on api.todos to web_anon; |
web_anon 角色拥有访问 api schema 的权限,可以读取 todos 表中的数据(rows)。
4.运行 PostgRES
(1)PostgREST 使用一个配置文件来确定如何连接数据库。创建一个文件 tutorial.conf 并加上如下内容:
db-uri = "postgres://postgres:mysecretpassword@localhost/postgres"<br />db-schema = "api"<br />db-anon-role = "web_anon" |
(2)在目录postgrest下执行
./postgrest tutorial.conf
(3)web服务启动起来
Listening on port 3000 Attempting to connect to the database... Connection successful<br /><br />打开一个新的 terminal (保持 PostgREST 依旧运行)。尝试对 todos 做一个 HTTP 请求。
<br />curl http://localhost:3000/todos |
<br /><br />
PostgreSQL神器之PostgREST(macos版本使用)
标签:mac lex false serve -- curl cti prim follow
本文系统来源:https://www.cnblogs.com/hanvans/p/12368181.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!