微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Docker - 无法从 pgadmin 连接到 posgres

如何解决Docker - 无法从 pgadmin 连接到 posgres

我使用以下 docker-compose 文件运行了 docker-compose 来运行 Postgres 和 pgadmin。 我可以登录 pgadmin 但 pgadmin 无法连接到 Postgres。但是,Postgres 运行平稳并接受请求。

docker-compose.yaml

version: "3"
services:
  postgres:
    image: postgres
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
    volumes:
      - postgres-data:/var/lib/postgresql/data

  pgadmin:
    image: dpage/pgadmin4
    ports:
      - 5050:80
    environment:
      - PGADMIN_DEFAULT_EMAIL=user@hotmail.com
      - PGADMIN_DEFAULT_PASSWORD=password

volumes:
  postgres-data:
    driver: local

这是从 pgadmin 视图页面连接 Postgres 时的错误

无法连接到服务器:

无法连接到服务器:连接被拒绝 服务器是否正在运行 在主机“localhost”(127.0.0.1)上并接受 TCP/IP 连接 5432端口?无法连接到服务器:地址不可用是 服务器在主机“localhost”(::1) 上运行并接受 TCP/IP 端口 5432 上的连接?

enter image description here

这是 docker-compose 日志。

db_1            | Postgresql init process complete; ready for start up.
db_1            | 
db_1            | 2021-02-02 04:57:59.825 UTC [1] LOG:  starting Postgresql 12.5 (Debian 12.5-1.pgdg100+1) on x86_64-pc-linux-gnu,compiled by gcc (Debian 8.3.0-6) 8.3.0,64-bit
db_1            | 2021-02-02 04:57:59.825 UTC [1] LOG:  listening on IPv4 address "0.0.0.0",port 5432
db_1            | 2021-02-02 04:57:59.825 UTC [1] LOG:  listening on IPv6 address "::",port 5432
db_1            | 2021-02-02 04:57:59.842 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGsql.5432"
db_1            | 2021-02-02 04:57:59.867 UTC [76] LOG:  database system was shut down at 2021-02-02 04:57:59 UTC
db_1            | 2021-02-02 04:57:59.877 UTC [1] LOG:  database system is ready to accept connections
pgadmin_1       | NOTE: Configuring authentication for SERVER mode.
pgadmin_1       | 
pgadmin_1       | [2021-02-02 04:58:10 +0000] [1] [INFO] Starting gunicorn 19.9.0
pgadmin_1       | [2021-02-02 04:58:10 +0000] [1] [INFO] Listening at: http://[::]:5050 (1)
pgadmin_1       | [2021-02-02 04:58:10 +0000] [1] [INFO] Using worker: threads
pgadmin_1       | /usr/local/lib/python3.9/os.py:1023: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode,the default buffer size will be used
pgadmin_1       |   return io.open(fd,*args,**kwargs)
pgadmin_1       | [2021-02-02 04:58:10 +0000] [87] [INFO] Booting worker with pid: 87
pgadmin_1       | ::ffff:192.168.80.1 - - [02/Feb/2021:05:34:52 +0000] "GET /browser/ HTTP/1.1" 302 257 "http://localhost:5050/login?next=%2Fbrowser%2F" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/88.0.4324.96 Safari/537.36"
pgadmin_1       | ::ffff:192.168.80.1 - - [02/Feb/2021:05:34:52 +0000] "GET /login?next=%2Fbrowser%2F HTTP/1.1" 200 1707 "http://localhost:5050/login?next=%2Fbrowser%2F" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/88.0.4324.96 Safari/537.36"
pgadmin_1       | 2021-02-02 05:34:52,568: ERROR    flask.app:  404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
pgadmin_1       | Traceback (most recent call last):
pgadmin_1       |   File "/usr/local/lib/python3.9/site-packages/flask/app.py",line 1813,in full_dispatch_request
pgadmin_1       |     rv = self.dispatch_request()
pgadmin_1       |   File "/usr/local/lib/python3.9/site-packages/flask/app.py",line 1791,in dispatch_request
pgadmin_1       |     self.raise_routing_exception(req)
pgadmin_1       |   File "/usr/local/lib/python3.9/site-packages/flask/app.py",line 1774,in raise_routing_exception
pgadmin_1       |     raise request.routing_exception
pgadmin_1       |   File "/usr/local/lib/python3.9/site-packages/flask/ctx.py",line 336,in match_request
pgadmin_1       |     self.url_adapter.match(return_rule=True)
pgadmin_1       |   File "/usr/local/lib/python3.9/site-packages/werkzeug/routing.py",line 1945,in match
pgadmin_1       |     raise NotFound()
pgadmin_1       | werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

我不知道哪里出了问题。

解决方法

在集群内部时,您的容器使用服务名称相互通信。在您的情况下,您的服务名称是 postgres

错误表明您正在尝试将服务器名称设置为 localhost。您需要进行以下设置:

enter image description here

,

尝试创建一个网络并将两个容器都放在上面:

version: "3"

networks:
  bridge_net:
    driver: bridge

services:
  postgres:
    image: postgres
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
    volumes:
      - postgres-data:/var/lib/postgresql/data
    networks:
      - bridge_net

  pgadmin:
    image: dpage/pgadmin4
    ports:
      - 5050:80
    environment:
      - PGADMIN_DEFAULT_EMAIL=user@hotmail.com
      - PGADMIN_DEFAULT_PASSWORD=password
    networks:
      - bridge_net

volumes:
  postgres-data:
    driver: local

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。