安装#

发布页面上有 pre-compiled binaries for macOS, Windows, Linux and FreeBSD 。Linux二进制文件是可以在任何Linux发行版上运行的静态可执行文件。

您也可以使用您的操作系统包管理器。

可以从以下位置安装PostgREST Homebrew official repo

brew install postgrest

支持的PostgreSQL版本#

Supported

PostgreSQL>=9.6

PostgREST适用于从9.6开始的所有PostgreSQL版本。

正在运行PostgREST#

如果您从发布页面下载了PostgREST,请首先解压压缩文件以获得可执行文件。

# For UNIX platforms
tar Jxf postgrest-[version]-[platform].tar.xz

# On Windows you should unzip the file

现在,您可以使用 --help 查看使用说明的标志:

# Running postgrest binary
./postgrest --help

# Running postgrest installed from a package manager
postgrest --help

# You should see a usage help message

PostgREST服务器读取一个配置文件作为其唯一参数:

postgrest /path/to/postgrest.conf

# You can also generate a sample config file with
# postgrest -e > postgrest.conf
# You'll need to edit this file and remove the usage parts for postgrest to read it

有关配置文件的完整参考,请参见 配置

备注

如果您在Windows上看到这样的对话框,则可能是 pg_config 程序不在您的系统路径中。

../_images/win-err-dialog.png

It usually lives in C:Program FilesPostgreSQL<version>bin. See this article about how to modify the system path.

要测试系统路径是否设置正确,请运行 pg_config 从命令行。您应该看到它输出了一个路径列表。

多克尔#

你可以拿到 official PostgREST Docker image 有:

docker pull postgrest/postgrest

要配置容器镜像,请使用 环境变量

有两种方式可以运行PostgREST容器:使用现有的外部数据库,或者通过docker-compose。

使用原生PostgreSQL的集装化PostgREST#

在Docker中运行PostgREST的第一种方式是将其连接到主机上现有的本地数据库。

# Run the server
docker run --rm --net=host \
  -e PGRST_DB_URI="postgres://app_user:password@localhost/postgres" \
  postgrest/postgrest

上面的数据库连接字符串只是一个示例。根据需要调整角色和密码。您可能需要编辑PostgreSQL pg_hba.conf 授予用户本地登录访问权限。

备注

Mac上的Docker不支持 --net=host 旗帜。相反,您需要为主机创建一个IP地址别名。来自容器内部的IP地址请求无法解析,并回退到主机解析。

sudo ifconfig lo0 10.0.0.10 alias

然后,您应该在数据库连接字符串中使用10.0.0.10作为主机。还请记住,在 listen_address 在postgresql.conf中。例如:

listen_addresses = 'localhost,10.0.0.10'

您可能还需要在pg_hba.conf中添加新的IPv4本地连接。例如:

host    all             all             10.0.0.10/32            trust

然后,docker命令将如下所示:

# Run the server
docker run --rm -p 3000:3000 \
  -e PGRST_DB_URI="postgres://app_user:password@10.0.0.10/postgres" \
  postgrest/postgrest

集装箱化邮政 and 带坞站的数据库-Compose#

为了完全避免安装数据库,您可以在容器中运行它和服务器,并使用docker-compose将它们链接在一起。使用此配置:

# docker-compose.yml

version: '3'
services:
  server:
    image: postgrest/postgrest
    ports:
      - "3000:3000"
    environment:
      PGRST_DB_URI: postgres://app_user:password@db:5432/app_db
      PGRST_OPENAPI_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:
  #   - "./pgdata:/var/lib/postgresql/data"

进入保存此文件的目录,然后运行 docker-compose up 。您将看到数据库和PostgREST的日志,并能够在端口3000上访问后者。

如果您想要在浏览器中获得API的可视概览,可以将swagger-UI添加到 docker-compose.yml

# in services:
  swagger:
    image: swaggerapi/swagger-ui
    ports:
      - "8080:8080"
    expose:
      - "8080"
    environment:
      API_URL: http://localhost:3000/

这样,您就可以在您的浏览器的端口8080上看到swagger-UI。

从源开始构建#

当您的系统不存在预构建的二进制文件时,您可以从源代码构建项目。

您可以使用以下命令从源代码构建PostgREST Stack 。它将在您的系统上安装任何必要的Haskell依赖项。

  • Install Stack 适用于您的平台

  • 安装库依赖项

    操作系统

    相依性

    Ubuntu/Debian

    Libpq-dev、libgmp-dev、zlib1g-dev

    CentOS/Fedora/Red Hat

    PostgreSQL-devel、zlib-devel、GMP-devel

    BSD

    Postgresql12-客户端

    MacOS

    Libpq,GMP

  • 构建并安装二进制文件

    git clone https://github.com/PostgREST/postgrest.git
    cd postgrest
    
    # adjust local-bin-path to taste
    stack build --install-ghc --copy-bins --local-bin-path /usr/local/bin
    

备注

  • 如果构建失败,并且您的系统内存不足1 GB,请尝试添加交换文件。

  • --install-ghc 标志只在第一次生成时需要,在后续生成中可以省略。

  • 检查服务器是否已安装: postgrest --help