一次安装步骤(使用docker-compose)

  1. version: '3'
  2. services:
  3. server:
  4. image: postgrest/postgrest
  5. ports:
  6. - "3000:3000"
  7. links:
  8. - db:db
  9. environment:
  10. PGRST_DB_URI: postgres://app_user:password@db:5432/app_db
  11. PGRST_DB_SCHEMA: public
  12. PGRST_DB_ANON_ROLE: app_user #In production this role should not be the same as the one used for the connection
  13. PGRST_SERVER_PROXY_URI: "http://127.0.0.1:3000"
  14. depends_on:
  15. - db
  16. db:
  17. image: postgres
  18. ports:
  19. - "5432:5432"
  20. environment:
  21. POSTGRES_DB: app_db
  22. POSTGRES_USER: app_user
  23. POSTGRES_PASSWORD: password
  24. # Uncomment this if you want to persist the data.
  25. volumes:
  26. - "~/postgrest/pgdata:/var/lib/postgresql/data"

docker-compose -up 运行

分开步骤

docker安装postgresql

  1. # 安装并后台运行postgresql
  2. sudo docker run --name postgres -p 5432:5432 \
  3. -e POSTGRES_PASSWORD=123456 \
  4. -d postgres
  5. # 进入postgres
  6. sudo docker exec -it postgres /bin/bash

进入数据库,创建对应模式和表

  1. sudo su - postgres
  2. psql
  3. # 创建模式
  4. create schema api;
  5. # 创建表
  6. create table api.todos (
  7. id serial primary key,
  8. done boolean not null default false,
  9. task text not null,
  10. due timestamptz
  11. );
  12. # 写入2条数据
  13. insert into api.todos (task) values ('finish tutorial 0'), ('pat self on back');

创建访问模式的角色

  1. create role web_anon nologin;
  2. grant usage on schema api to web_anon;
  3. grant select on api.todos to web_anon;
  4. create role authenticator noinherit login password '123456';
  5. grant web_anon to authenticator;

docker安装postgrest

  1. sudo docker run --name postgrest -p 3000:3000 \
  2. -v ~/config:/etc/etc/postgrest/config
  3. -d postgrest/postgrest:latest

手动安装postgrest

  1. cd ~
  2. wget https://github.com/PostgREST/postgrest/releases/download/v7.0.1/postgrest-v7.0.1-linux-x64-static.tar.xz
  3. tar xJf postgrest-v7.0.1-linux-x64-static.tar.xz
  4. mv postgrest /usr/local/bin

如果提示wget不存在,运行下面命令安装wget apt update && apt install -y wget

创建postgrest配置文件

  1. sudo mkdir /etc/postgrest
  2. sudo vi /etc/postgrest/config
  3. ### 配置文件内容
  4. db-uri = "postgres://authenticator:123456@localhost:5432/postgres"
  5. db-schema = "api"
  6. db-anon-role = "web_anon"
  7. ###
  8. # 运行postgrest
  9. /usr/local/bin/postgrest /etc/postgrest/config
  10. # 访问 http://localhost:3000/todos
  11. curl http://localhost:3000/todos

如果提示vi不存在,则运行如下命令 apt install -y vim

image.png