PostgreSQLサーバのクエリーをWebAPIサービスにする、PostgREST。
クエリをクライアント側で記述したい場合とても便利です。簡単な導入までをメモしました。
参考)https://postgrest.org/en/stable/tutorials/tut0.html#step-4-run-postgrest
環境)PostgreSQL / Mac
インストール)
brew install postgrest
PostgreSQL側設定
create role web_anon nologin;
grant usage on schema public to web_anon;
grant select on public.users to web_anon;
~
postgrest.conf
db-uri = “postgres://postgres:postgres@localhost:5432/mydb”
db-schemas = “public”
db-anon-role = “web_anon”
psql
mydb=# select * from users;
id | name | email | created_at | updated_at
—————————+——–+——————+————————-+————————-
clu93qyv50001cbapfs2wc3gt | name01 | name01@email.com | 2024-03-27 01:03:28.769 | 2024-03-27 01:03:09.071
(1 row)
起動
postgrest postgrest.conf
アクセス
% curl localhost:3000/users
{“code”:”PGRST302″,”details”:null,”hint”:null,”message”:”Anonymous access is disabled”}% curl localhost:3000/users
[{“id”:”clu93qyv50001cbapfs2wc3gt”,”name”:”name01″,”email”:”name01@email.com”,”created_at”:”2024-03-27T01:03:28.769″,”updated_at”:”2024-03-27T01:03:09.071″}]
簡単なアクセステストでした。
JWTを使った認可もできますが、また次の機会に試したいと思います。