![](/img/trans.png)
[英]psql: no pg_hba.conf entry for host, could not connect to server
[英]How can I allow connections by specifying docker-compose host names in postgres's pg_hba.conf file?
我試圖通過在服務器的pg_hba.conf
文件中指定客戶端容器的主機名來允許從一個 Docker 容器連接到 postgres 容器。 Postgres 的文檔表明可以指定主機名,而不是 IP 地址。 由於我使用 Docker Compose 來啟動兩個容器,因此它們應該可以使用 Docker Compose 的 DNS 通過容器名稱相互訪問。 出於安全原因,我不想打開所有 IP 地址,當我最終為其他容器添加訪問權限時,在 pg_hba.conf 文件中指定容器名稱而不是為每個容器分配靜態 IP 地址會容易得多其中。 但是,當我嘗試執行此操作時,它失敗並顯示如下消息:
psql: FATAL: no pg_hba.conf entry for host "192.168.208.3", user "postgres", database "postgres", SSL off
這是我正在嘗試做的最小可重復示例:
我使用以下 Docker Compose 文件:
version: '3'
services:
postgresdb:
image: postgres:9.4
container_name: postgres-server
ports:
- "5432:5432"
volumes:
- "postgres-data:/var/lib/postgresql/data"
postgres-client:
image: postgres:9.4
container_name: postgres-client
depends_on:
- postgres-server
volumes:
postgres-data:
運行docker-compose up
,我執行到服務器容器並修改/var/lib/postgresql/data
的pg_hba.conf
文件,如下所示:
host all postgres postgres-client trust
然后我重新啟動 postgres 服務器( docker-compose down
然后docker-compose up
)並從安裝的卷加載修改后的pg_hba.conf
。
我執行到客戶端容器並嘗試連接到 postgres 服務器:
docker exec -it postgres-client /bin/bash
psql -U postgres -h postgres-server postgres
這是我收到如下錯誤的地方:
psql: FATAL: no pg_hba.conf entry for host "192.168.208.3", user "postgres", database "postgres", SSL off
我似乎無法在網上找到任何顯示如何使其工作的內容。 我找到了一些例子,它們只是打開所有或一系列 IP 地址,但沒有一個例子可以使用主機名。 以下是一些相關的問題和信息:
關於如何使用 Docker Compose 以我期望的方式使其工作的任何想法?
您需要在pg_hba.conf
添加客戶端容器的完全限定主機名。
host all postgres postgres-client.<network_name> trust
例如:
host all postgres postgres-client.postgreshostresolution_default trust
如果尚未定義網絡,則network_name
為<project_name>_default
。 默認情況下, project_name
是 docker-compose.yml 所在的文件夾。
要獲取網絡名稱,您也可以調用
docker inspect postgres-client | grep Networks -A1
或者
docker network ls
獲取當前在您的 docker 主機上定義的所有 docker 網絡的列表
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.