一次安装步骤(使用docker-compose)
version: '3'
services:
server:
image: postgrest/postgrest
ports:
- "3000:3000"
links:
- db:db
environment:
PGRST_DB_URI: postgres://app_user:password@db:5432/app_db
PGRST_DB_SCHEMA: public
PGRST_DB_ANON_ROLE: app_user #In production this role should not be the same as the one used for the connection
PGRST_SERVER_PROXY_URI: "http://127.0.0.1:3000"
depends_on:
- db
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_DB: app_db
POSTGRES_USER: app_user
POSTGRES_PASSWORD: password
# Uncomment this if you want to persist the data.
volumes:
- "~/postgrest/pgdata:/var/lib/postgresql/data"
docker-compose -up 运行
分开步骤
docker安装postgresql
# 安装并后台运行postgresql
sudo docker run --name postgres -p 5432:5432 \
-e POSTGRES_PASSWORD=123456 \
-d postgres
# 进入postgres
sudo docker exec -it postgres /bin/bash
进入数据库,创建对应模式和表
sudo su - postgres
psql
# 创建模式
create schema api;
# 创建表
create table api.todos (
id serial primary key,
done boolean not null default false,
task text not null,
due timestamptz
);
# 写入2条数据
insert into api.todos (task) values ('finish tutorial 0'), ('pat self on back');
创建访问模式的角色
create role web_anon nologin;
grant usage on schema api to web_anon;
grant select on api.todos to web_anon;
create role authenticator noinherit login password '123456';
grant web_anon to authenticator;
docker安装postgrest
sudo docker run --name postgrest -p 3000:3000 \
-v ~/config:/etc/etc/postgrest/config
-d postgrest/postgrest:latest
手动安装postgrest
cd ~
wget https://github.com/PostgREST/postgrest/releases/download/v7.0.1/postgrest-v7.0.1-linux-x64-static.tar.xz
tar xJf postgrest-v7.0.1-linux-x64-static.tar.xz
mv postgrest /usr/local/bin
如果提示wget不存在,运行下面命令安装wget apt update && apt install -y wget
创建postgrest配置文件
sudo mkdir /etc/postgrest
sudo vi /etc/postgrest/config
### 配置文件内容
db-uri = "postgres://authenticator:123456@localhost:5432/postgres"
db-schema = "api"
db-anon-role = "web_anon"
###
# 运行postgrest
/usr/local/bin/postgrest /etc/postgrest/config
# 访问 http://localhost:3000/todos
curl http://localhost:3000/todos
如果提示vi不存在,则运行如下命令 apt install -y vim