postgrest配置及说明

postgrest配置及说明

1
2
3
4
5
# Allow "tr" to process non-utf8 byte sequences
export LC_CTYPE=C

# Read random bytes keeping only alphanumerics and add the secret to the configuration file
echo "jwt-secret = \"$(< /dev/urandom tr -dc A-Za-z0-9 | head -c32)\""
  • 记住这里的jwt-secret的内容,后面放到docker-compose的PGRST_JWT_SECRET中
  • 打开https://jwt.io/
  • payload中写{role: web_user}
  • secret写入这里的jwt-secret的内容

    img

  • 记下左边生成的token,作为每次请求的身份认证
  • postgres
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
version: '3'
services:
  postgres:
    image: postgres
    container_name: postgres
    restart: always
    # set shared memory limit when using docker-compose
    shm_size: 128mb
    environment:
      TZ: Asia/Shanghai
      POSTGRES_PASSWORD: ${PGSQL_ROOT}
    ports:
      - "5432:5432"
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./data:/var/lib/postgresql/data:rw
      - ./customize.conf:/etc/postgresql/postgresql.conf.d/custmize.conf

  adminer:
    image: adminer
    restart: always
    ports:
      - 8089:8080
  • postgrest
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
version: '3'
services:
  pgrest:
    image: postgrest/postgrest
    ports:
      - "3000:3000"
    environment:
      PGRST_DB_URI: postgres://henry:${PGSQL_PWD}@${PGSQL_HOST}:5432/web
      PGRST_JWT_SECRET: sample_jwt_secret
      PGRST_OPENAPI_SERVER_PROXY_URI: http://127.0.0.1:3000
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## 如果没有普通用户,先用postgres用户登录,创建普通用户
create role henry noinherit login password 'sample_pwd';

## 然后创建web_user用于读写数据
create role web_user nologin;
## 指定henry可以切换用户为web_user
## 因为henry在PGRST_DB_URI中,因此postgrest用henry身份连接到postgres,然后henry切换用户为web_user.
grant web_user to henry;

## 切换到指定的数据库
## 将schema和表的权限给web_user
grant usage on schema public to web_user;
grant all on public.msfund to web_user;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
export token=sample_token
set-variable -name token -value sample_token

curl http://10.8.0.68:3000/msfund -X POST -H "Authorization: Bearer $token" -H "Content-Type: application/json" -d '{"code": "002644", "data": "{\"FundName\":\"大成景荣债券A\", \"CategoryName\": \"普通债券\"}"}'

curl http://10.8.0.68:3000/msfund -H "Authorization: Bearer $token" -H "Content-Type: application/json"

curl http://10.8.0.68:3000/msfund?code=eq.002644 -H "Authorization: Bearer $token" -H "Content-Type: application/json"

curl http://10.8.0.68:3000/msfund?code=eq.002644 -X PUT -H "Authorization: Bearer $token" -H "Content-Type: application/json" -d '{"code": "002644", "data": "{\"FundName\":\"易方达中小盘A\", \"CategoryName\": \"大盘价值\"}"}'

curl http://10.8.0.68:3000/msfund?code=eq.002644 -X PATCH -H "Authorization: Bearer $token" -H "Content-Type: application/json" -d '{"code": "002644", "data": "{\"FundName\":\"易方达优质精选A\", \"CategoryName\": \"大盘价值\"}"}'

curl -X DELETE "http://10.8.0.68:3000/msfund?code=eq.002644" -H "Authorization: Bearer $token"