跳到主要内容

Hello PostgREST

1. 环境

2. 数据库准备

# 将数据存储于本地该目录
mkdir -p ~/data/pg
# 初次启动
# 启动完成后 Ctrl+C 终止
docker run -it --rm -v $HOME/data/pg:/var/lib/postgresql/data -e POSTGRES_PASSWORD=OP6Ats postgres:alpine

# 再次启动, 使用已经初始化好的数据目录, 此时不再需要指定密码, 以后启动均使用该命令
docker run -it --rm -v $HOME/data/pg:/var/lib/postgresql/data -p 5432:5432 postgres:alpine

# 查看当前版本 并确认服务正常启动
# 需要输入之前的密码 OP6Ats
psql -h 127.0.0.1 -U postgres -c 'show server_version'

# 创建易开业数据库
psql -h 127.0.0.1 -U postgres -c 'create database yky'
# 创建服务系统 schema
psql -h 127.0.0.1 -U postgres yky -c 'create schema servicer'

# 切换到工作目录
cd ~/data/pg

# PostgREST
# ---------
# 配置文件
cat << CONF > postgrest.conf
db-uri = "postgres://postgres:[email protected]/yky"
db-schema = "servicer"
db-anon-role = "postgres"
CONF
# 启动 PostgREST
# http://localhost:3000 为 rest 服务接口
docker run --rm -p 3000:3000 -v $PWD:/host --name postgrest postgrest/postgrest postgrest /host/postgrest.conf
# 启动 SwaggerUI
# http://localhost:8081 为 swagger ui
docker run --rm -p 8081:8080 -e API_URL=http://localhost:3000 swaggerapi/swagger-ui

3. 数据控制台

打开 DataGrip, 新增 PostgreSQL 数据源, 主机 localhost, 端口 5432, 账号 postgres 密码 OP6Ats 数据库 yky

打开控制台, 执行 SQL

-- 设置搜索路径, 主要修改当前 schema
set search_path=servicer,public;


-- 客户
create table customer (
id int generated by default as identity,

-- 基础信息
name text not null,
type text not null, -- 拟设立, 已设立
email text,
wechat text,
qq text,

source text not null,
state text,
remark text,

addition jsonb,
--
primary key (id)
);

执行完成后要让 PostgREST 从新扫描数据库

docker kill -s HUP postgrest

刷新 http://localhost:8081 即可看到新的接口, 往后的操作便是逐渐完善数据库和熟悉产品数据模型.