簡體   English   中英

Postgres 不允許 `127.0.0.1` 但可以與 `localhost` 一起使用

[英]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會失敗? 我在下面提供了詳細信息。 還應該檢查什么?

精確重播命令

運行容器pgApgB

> 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

pgApgB兩個容器的比較

  • 作為/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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM