[英]Postgres not allowing `127.0.0.1` but works with `localhost`
Postgres 在容器中運行,使用postgres:14.1-alpine
映像。
當使用--network host
運行 A) 時,然后
PGPASSWORD=postgres psql -d db -U postgres -h localhost # works
PGPASSWORD=postgres psql -d db -U postgres -h 127.0.0.1 # fails
當在沒有--network host
的情況下運行 B) 時,上面的兩個主機都可以工作。
錯誤是
psql: error: connection to server at "127.0.0.1", port 5432 failed:
FATAL: password authentication failed for user "postgres"
為什么A)中的-h 127.0.0.1
會失敗? 我在下面提供了詳細信息。 還應該檢查什么?
運行容器pgA和pgB :
> docker run -d --rm -it --network host \
-e POSTGRES_PASSWORD=postgres -ePOSTGRES_USER=postgres -e POSTGRES_DB=db \
--name pgA \
postgres:14.1-alpine
OUTPUT: <containerA_id>
> docker run -d --rm -it \
-e POSTGRES_PASSWORD=postgres -ePOSTGRES_USER=postgres -e POSTGRES_DB=db \
--name pgB \
postgres:14.1-alpine
OUTPUT: <containerB_id>
嘗試使用-h 127.0.0.1
連接到兩者:
> docker exec -it \
pgA \
bash -c ' PGPASSWORD=postgres psql -h 127.0.0.1 -d db -U postgres'
OUTPUT: psql: error: connection to server at "127.0.0.1", port 5432 failed:
FATAL: password authentication failed for user "postgres"
> docker exec -it \
pgB \
bash -c ' PGPASSWORD=postgres psql -h 127.0.0.1 -d db -U postgres'
OUTPUT: psql (14.1)
Type "help" for help.
db=#
> docker --version
Docker version 20.10.11, build dea9396
> uname -a
Darwin foo.local 20.6.0 Darwin Kernel Version 20.6.0:
Mon Aug 30 06:12:20 PDT 2021; root:xnu-7195.141.6~3/RELEASE_ARM64_T8101 arm64
pgA
和pgB
兩個容器的比較作為/var/lib/postgresql/data/
的直接后代的所有文件都是相同的(除了postmaster.pid
中的不同 PID)。 例如, postgresql.conf
包含
listen_addresses = '*'
在A)和B)容器中。
同樣, pg_hba.conf
是相同的
local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all::1/128 trust # Allow replication connections from localhost, by a user with the # replication privilege. local replication all trust host replication all 127.0.0.1/32 trust host replication all::1/128 trust
( /var/lib/postgresql/data/
的子目錄,例如pg_wal/
, base/
等可能不同 - 尚未檢查。)
netstat -anpt | grep 5432
netstat -anpt | grep 5432
相同
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN -
tcp 0 0 :::5432 :::* LISTEN
/etc/hosts
幾乎相同... 127.0.0.1 localhost::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters
...除了B) 與--network bridge
,此行附加: <container_ip> <container_id>
這是一個愚蠢的疏忽,沒有意識到另一個,第三個 PG 實例已經橋接了HostPort
5432。
感謝@jjanes 提供的日志和 IPv6 提示。 為了后代,請檢查您的日志!
LOG: could not bind IPv4 address "0.0.0.0": Address in use
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.