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

sshcrbum  于 2023-11-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(166)

我不能从容器外部访问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

u7up0aaq

u7up0aaq1#

Postgres默认配置为侦听端口5432。这意味着在端口配置中,如果您想将本地计算机连接到postgres Docker容器,则本地计算机将通过localhost访问的postgres Docker容器的端口必须为5432。这是真的,除非docker postgres中的端口配置被更改。本地机器中的可用端口可以是任何端口。这导致我更改compose.yaml中的port行:

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=#

相关问题