无法将 pgadmin 连接到在 docker-compose 初始化的 docker 容器内运行的 postgresql

问题描述 投票:0回答:1

我无法从容器外部访问 postgresql 服务器,尽管我可以从容器内部访问。

我在 compose.yaml 文件中定义了以下变量:

services:
  postgres:
    container_name: postgredb
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: "PostGres2023!"
      POSTGRES_DB: potato
      POSTGRES_INITDB_ARGS: --auth=scram-sha-256
    ports:
      - 5432:5432

在意识到我无需密码即可从容器内部登录服务器后,我添加了 POSTGRES_INITDB_ARGS: --auth=scram-sha-256。

C:\Users\lucab>docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                    NAMES
db276a10e549   postgres   "docker-entrypoint.s…"   7 seconds ago   Up 2 seconds   0.0.0.0:5432->5432/tcp   postgredb

C:\Users\lucab>docker exec -it db276a10e549 bash
root@db276a10e549:/# psql -h localhost -U postgres
psql (16.0 (Debian 16.0-1.pgdg120+1))
Type "help" for help.

同时,我尝试在本地计算机上从 pgadmin 进行日志记录,这会产生一个烦人的密码错误:

PG-ADMIN SERVER REGISTER PROMPT

在“POSTGRES_INITDB_ARGS: --auth=scram-sha-256”行之后,我被要求从容器内部输入密码,并成功地使用 compose.yaml 文件中定义的值登录(注意“用户 postgres 的密码:"):

C:\Users\lucab>docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                    NAMES
5566cb874560   postgres   "docker-entrypoint.s…"   20 minutes ago   Up 20 minutes   0.0.0.0:5432->5432/tcp   postgredb

C:\Users\lucab>docker exec -it 5566cb874560 bash
root@5566cb874560:/# psql -h localhost -U postgres
Password for user postgres:
psql (16.0 (Debian 16.0-1.pgdg120+1))
Type "help" for help.

postgres=#

我认为这可以解决我的问题,因为 pgadmins 错误可能来自不需要密码的服务器,并且除非提供密码,否则 pgadmin 不会连接。显然情况并非如此。对于任何密码值,我在 pgadmin 中输入的错误都是相同的。如果我将密码提示留空,它将返回:

PG-ADMIN SERVER REGISTER PROMPT

为了进一步调试,这里是容器日志:

2023-10-29 18:56:53 The files belonging to this database system will be owned by user "postgres".
2023-10-29 18:56:53 This user must also own the server process.
2023-10-29 18:56:53 
2023-10-29 18:56:53 The database cluster will be initialized with locale "en_US.utf8".
2023-10-29 18:56:53 The default database encoding has accordingly been set to "UTF8".
2023-10-29 18:56:53 The default text search configuration will be set to "english".
2023-10-29 18:56:53 
2023-10-29 18:56:53 Data page checksums are disabled.
2023-10-29 18:56:53 
2023-10-29 18:56:53 fixing permissions on existing directory /var/lib/postgresql/data ... ok
2023-10-29 18:56:53 creating subdirectories ... ok
2023-10-29 18:56:53 selecting dynamic shared memory implementation ... posix
2023-10-29 18:56:53 selecting default max_connections ... 100
2023-10-29 18:56:53 selecting default shared_buffers ... 128MB
2023-10-29 18:56:53 selecting default time zone ... Etc/UTC
2023-10-29 18:56:53 creating configuration files ... ok
2023-10-29 18:56:53 running bootstrap script ... ok
2023-10-29 18:56:53 performing post-bootstrap initialization ... ok
2023-10-29 18:56:54 syncing data to disk ... ok
2023-10-29 18:56:54 
2023-10-29 18:56:54 Success. You can now start the database server using:
2023-10-29 18:56:54 
2023-10-29 18:56:54     pg_ctl -D /var/lib/postgresql/data -l logfile start
2023-10-29 18:56:54 
2023-10-29 18:56:54 waiting for server to start....2023-10-29 21:56:54.105 UTC [48] LOG:  starting PostgreSQL 16.0 (Debian 16.0-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2023-10-29 18:56:54 2023-10-29 21:56:54.108 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-10-29 18:56:54 2023-10-29 21:56:54.115 UTC [51] LOG:  database system was shut down at 2023-10-29 21:56:53 UTC
2023-10-29 18:56:54 2023-10-29 21:56:54.119 UTC [48] LOG:  database system is ready to accept connections
2023-10-29 18:56:54  done
2023-10-29 18:56:54 server started
2023-10-29 18:56:54 CREATE DATABASE
2023-10-29 18:56:54 
2023-10-29 18:56:54 
2023-10-29 18:56:54 /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
2023-10-29 18:56:54 
2023-10-29 18:56:54 waiting for server to shut down....2023-10-29 21:56:54.307 UTC [48] LOG:  received fast shutdown request
2023-10-29 18:56:54 2023-10-29 21:56:54.310 UTC [48] LOG:  aborting any active transactions
2023-10-29 18:56:54 2023-10-29 21:56:54.311 UTC [48] LOG:  background worker "logical replication launcher" (PID 54) exited with exit code 1
2023-10-29 18:56:54 2023-10-29 21:56:54.311 UTC [49] LOG:  shutting down
2023-10-29 18:56:54 2023-10-29 21:56:54.314 UTC [49] LOG:  checkpoint starting: shutdown immediate
2023-10-29 18:56:54 2023-10-29 21:56:54.388 UTC [49] LOG:  checkpoint complete: wrote 923 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.014 s, sync=0.054 s, total=0.077 s; sync files=301, longest=0.015 s, average=0.001 s; distance=4257 kB, estimate=4257 kB; lsn=0/19130D0, redo lsn=0/19130D0
2023-10-29 18:56:54 2023-10-29 21:56:54.393 UTC [48] LOG:  database system is shut down
2023-10-29 18:56:54  done
2023-10-29 18:56:54 server stopped
2023-10-29 18:56:54 
2023-10-29 18:56:54 PostgreSQL init process complete; ready for start up.
2023-10-29 18:56:54 
2023-10-29 18:56:54 2023-10-29 21:56:54.428 UTC [1] LOG:  starting PostgreSQL 16.0 (Debian 16.0-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2023-10-29 18:56:54 2023-10-29 21:56:54.428 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-10-29 18:56:54 2023-10-29 21:56:54.428 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-10-29 18:56:54 2023-10-29 21:56:54.433 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-10-29 18:56:54 2023-10-29 21:56:54.439 UTC [64] LOG:  database system was shut down at 2023-10-29 21:56:54 UTC
2023-10-29 18:56:54 2023-10-29 21:56:54.444 UTC [1] LOG:  database system is ready to accept connections
2023-10-29 18:57:46 2023-10-29 21:57:46.709 UTC [77] FATAL:  password authentication failed for user "postgres"
2023-10-29 18:57:46 2023-10-29 21:57:46.709 UTC [77] DETAIL:  Connection matched file "/var/lib/postgresql/data/pg_hba.conf" line 115: "host    all             all             127.0.0.1/32            scram-sha-256"
2023-10-29 18:58:01 2023-10-29 21:58:01.424 UTC [81] FATAL:  password authentication failed for user "postgres"
2023-10-29 18:58:01 2023-10-29 21:58:01.424 UTC [81] DETAIL:  Connection matched file "/var/lib/postgresql/data/pg_hba.conf" line 115: "host    all             all             127.0.0.1/32            scram-sha-256"
2023-10-29 19:01:54 2023-10-29 22:01:54.538 UTC [62] LOG:  checkpoint starting: time
2023-10-29 19:01:58 2023-10-29 22:01:58.789 UTC [62] LOG:  checkpoint complete: wrote 45 buffers (0.3%); 0 WAL file(s) added, 0 removed, 0 recycled; write=4.217 s, sync=0.022 s, total=4.251 s; sync files=12, longest=0.008 s, average=0.002 s; distance=261 kB, estimate=261 kB; lsn=0/1954540, redo lsn=0/1954508

postgresql docker docker-compose pgadmin
1个回答
0
投票

Postgres 默认配置为监听 5432 端口。这意味着在端口配置中,如果您要将本地计算机连接到 docker 容器,则本地计算机将访问的 docker 容器的端口必须为 5432。除非 docker postgres 中的端口配置发生更改,否则这是正确的。本地计算机上的可用端口可以是任何端口。这导致我更改 compose.yaml 中的端口行:

services:
  postgres:
    container_name: postgredb
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: "PostGres2023!"
      POSTGRES_DB: potato
    ports:
      - 8039:5432

这会导致 psql 内的成功连接:

Server [localhost]:
Database [postgres]:
Port [5432]: 8039
Username [postgres]:
Password for user postgres:
psql (16.0)
WARNING: Console code page (437) 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.

postgres=#
© www.soinside.com 2019 - 2024. All rights reserved.