昨天看到一个工具PostgREST:能把PostgreSQL的表以json格式读取出来,觉得很有意思,今天就看了下。
这个工具的主页在https://github.com/PostgREST
0.生成文档:
可以直接在这看: https://postgrest.org/en/stable/index.html
不过我按文档自己生成了一遍,步骤在https://github.com/PostgREST/postgrest-docs
不过github上文档不能直接看,需要使用Sphinx (https://pypi.org/project/Sphinx/)生成到本地。
按介绍安装生成,看了。
pip install -U sphinx
git clone git://github.com/PostgREST/postgrest-docs.git
cd postgrest-docs
sphinx-build -b html -a -n . _build
_build目录下就是文档了,打开_build/index.html就可以看了。
1.安装PostgREST
使用最新的v0.5或v0.4.4都可以
说明分别见以下link,下载后tar -xvf即可
https://github.com/PostgREST/postgrest/releases/tag/v0.5.0.0
wget -O 'postgrest-v0.5.0.0-centos6.tar.xz' 'https://github.com/PostgREST/postgrest/releases/download/v0.5.0.0/postgrest-v0.5.0.0-centos6.tar.xz'
https://github.com/PostgREST/postgrest/releases/tag/v0.4.4.0
wget -O 'postgrest-v0.4.4.0-centos6.tar.xz' 'https://github.com/PostgREST/postgrest/releases/download/v0.4.4.0/postgrest-v0.4.4.0-centos6.tar.xz'
2.启动服务数据库
启动命令是
./postgrest tutorial.conf
tutorial.conf是配置的postgresql信息:
db-uri = "postgres://postgres:password@sengtest/postgres"
db-schema = "api"
db-anon-role = "web_anon"
3.PostgreSQL版本过低导致问题
开始使用了CentOS 6自带的PostgreSQL8.4,觉得应该没问题,启动一直报这个错:数据库联不上 ,信息类似如下
{"details":"missing \"=\" after \"postgres://
开始怀疑权限有问题,修改了好多次,还没解决。
后来看了文档要9.4以上的PostgreSQL,估计要用到新版的client
好吧,重新安装PostgreSQL 10
4.安装PostgreSQL 10.4
前删除 yum remove postgresql
然后按照https://www.postgresql.org/download/linux/redhat/ 执行就可以了
安装完后,要配置参数文件,在/var/lib/pgsql/10/data下 ,由于测试不用过多考虑安全性,全部放开,可以这样设置
postgresql.conf 修改 listen_addresses = '*'
pg_hba.conf 增加host all all 0.0.0.0/0 md5
5.创建测试安装PostgreSQL用户
具体按照文档做就可以了
_build/tutorials/tut0.html#step-2-install-postgresql
6.测试基本功能
具体看_build/api.html#
测试了几个示例
#看所有记录
curl http://localhost:3000/todos
#过滤记录
curl 'http://localhost:3000/todos?id=gte.2&done=is.false'
#选择字段
curl 'http://localhost:3000/todos?select=id,task&id=gte.2&done=is.false'
#排序
curl 'http://localhost:3000/todos?select=id,task&order=id.desc&id=gte.1&done=is.false'
文档打包放在这里,可以直接看了.