如何将 Pact Broker 连接到 PostgreSQL 的本地或云实例?

How to connect the Pact Broker to a local or cloud instance of PostgreSQL?

我正在努力让 Pact Broker 运行ning 在 docker 容器中连接到我在 Windows.

上本地安装的 PostgreSQL

这就是我的 docker 运行 命令的样子...

docker run pactfoundation/pact-broker 
-e PACT_BROKER_DATABASE_USERNAME=pact_broker 
-e PACT_BROKER_DATABASE_PASSWORD={Password} 
-e PACT_BROKER_DATABASE_HOST=host.docker.internal 
-e PACT_BROKER_DATABASE_NAME=pact_broker 
-e PACT_BROKER_DATABASE_PORT=5432 
-p 9292:9292

其中returns出现以下错误...

Unable to load application: Sequel::DatabaseConnectionError: PG::ConnectionBad: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
bundler: failed to load command: puma (/pact_broker/vendor/bundle/ruby/2.6.0/bin/puma)
Sequel::DatabaseConnectionError: PG::ConnectionBad: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

这是我在 pgAdmin 4 中的数据库和用户

这是我用来测试连接的命令

psql postgresql://pact_broker:{Password}@localhost/pact_broker

哪个returns以下...

psql (13.3)
WARNING: Console code page (65001) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

pact_broker=>

我也试过运行宁下面的命令returns同样的错误

docker run pactfoundation/pact-broker -e PACT_BROKER_DATABASE_URL=postgresql://pact_broker:{Password}@host.docker.internal/pact_broker -p 9292:9292

我的 postgres.conf 文件包含 listen_addresses = '*',我的 pg_hba.conf 包含以下内容

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     scram-sha-256
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     scram-sha-256
host    all             all             0.0.0.0/0               trust
host    replication     all             ::1/128                 scram-sha-256
host    all             all             ::/0                    md5

我在使用 Azure Database for PostgreSQL 托管的实例时遇到了类似的问题

我的连接字符串如下所示...

psql "host=pactbrokerserver.postgres.database.azure.com port=5432 dbname={your_database} user=postgres@pactbrokerserver password={your_password} sslmode=require"

我试图通过本地协议代理连接到...

docker run pactfoundation/pact-broker 
-e PACT_BROKER_DATABASE_USERNAME=postgres@pactbrokerserver 
-e PACT_BROKER_DATABASE_PASSWORD={Password} 
-e PACT_BROKER_DATABASE_HOST=pactbrokerserver.postgres.database.azure.com 
-e PACT_BROKER_DATABASE_NAME=pactbroker 
-e PACT_BROKER_DATABASE_PORT=5432 
-e PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES=5 
-p 9292:9292

和它returns完全一样的错误。

这里可能有什么问题?

我认为这里发生的事情是您将容器名称 放在 环境参数列表之前 docker run 命令。

因此,不是使用您的自定义值为 运行ning 容器设置 PACT_BROKER_DATABASE_NAME 和其他环境变量,它们只是被 运行 时间丢弃。

试试这个:

docker run -e PACT_BROKER_DATABASE_USERNAME=pact_broker \
-e PACT_BROKER_DATABASE_PASSWORD={Password} \
-e PACT_BROKER_DATABASE_HOST=host.docker.internal \
-e PACT_BROKER_DATABASE_NAME=pact_broker \
-e PACT_BROKER_DATABASE_PORT=5432 \
-p 9292:9292 \
pactfoundation/pact-broker 

调试选项

万一我错了,问题可能与网络路径有关。为了排除这种情况,我们可以通过将入口点(通常是要启动的脚本和 运行 Web 服务器)替换为 /bin/sh 来将 shell 放入 docker 容器中:

docker run -it --entrypoint /bin/sh --user root -e PACT_BROKER_DATABASE_USERNAME=pact_broker -e PACT_BROKER_DATABASE_PASSWORD=Penwater858 -e PACT_BROKER_DATABASE_HOST=host.docker.internal -e PACT_BROKER_DATABASE_NAME=pact_broker -e PACT_BROKER_DATABASE_PORT=5432 -p 9292:9292 pactfoundation/pact-broker
# once in the container...
apk update
apk add busybox-extras
# see if there is a network path
telnet $PACT_BROKER_DATABASE_HOST $PACT_BROKER_DATABASE_PORT