为什么我无法在Docker中连接到Postgres?

kshnkvn

这个项目中使用了docker-compose两个docker容器均已成功启动。

kshnkvn@kshnkvn-vb:~$ docker ps
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                    NAMES
10fafbab73dc        openpoiservice_gunicorn_flask   "/ops_venv/bin/gunic…"   23 minutes ago      Up 22 minutes       0.0.0.0:5000->5000/tcp   openpoiservice_gunicorn_flask_1
a66fe5691455        kartoza/postgis:11.0-2.5        "/bin/sh -c /docker-…"   23 minutes ago      Up 22 minutes       5432/tcp                 openpoiservice_psql_postgis_db_1

但是,当尝试检查服务的功能时-他无法连接到数据库。我尝试手动执行此操作:

kshnkvn@kshnkvn-vb:~$ docker exec -it 10fafbab73dc /bin/bash
root@10fafbab73dc:/deploy/app# psql -h localhost -U gis_admin-gis
psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
root@10fafbab73dc:/deploy/app# 

奇怪,以防万一容器网络的类型是桥梁:

kshnkvn@kshnkvn-vb:~$ docker network ls
NETWORK ID          NAME                         DRIVER              SCOPE
81001dac99c0        bridge                       bridge              local
8e65fb4ef6f8        host                         host                local
94ce4e1605ef        none                         null                local
a3f48ac3facc        openpoiservice_default       bridge              local
e3d4286df013        openpoiservice_poi_network   bridge              local

已检查的postgres启动日志:

kshnkvn@kshnkvn-vb:~$ docker logs a66fe5691455
Add rule to pg_hba: 0.0.0.0/0
Add rule to pg_hba: replication replicator 
Setup master database
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
2020-02-08 13:50:20.675 UTC [25] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-02-08 13:50:20.683 UTC [25] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-02-08 13:50:20.756 UTC [37] LOG:  database system was interrupted; last known up at 2020-02-08 13:35:17 UTC
2020-02-08 13:50:21.830 UTC [48] postgres@postgres FATAL:  the database system is starting up
psql: FATAL:  the database system is starting up
2020-02-08 13:50:22.726 UTC [37] LOG:  database system was not properly shut down; automatic recovery in progress
2020-02-08 13:50:22.730 UTC [37] LOG:  redo starts at 0/21CCC50
2020-02-08 13:50:22.730 UTC [37] LOG:  invalid record length at 0/21CCC88: wanted 24, got 0
2020-02-08 13:50:22.730 UTC [37] LOG:  redo done at 0/21CCC50
2020-02-08 13:50:22.867 UTC [25] LOG:  database system is ready to accept connections
                              List of databases
   Name    |   Owner   | Encoding | Collate |  Ctype  |   Access privileges   
-----------+-----------+----------+---------+---------+-----------------------
 gis       | gis_admin | UTF8     | C.UTF-8 | C.UTF-8 | 
 postgres  | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | 
 template0 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
 template1 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
(4 rows)

postgres ready
Setup postgres User:Password
Creating superuser gis_admin
ALTER ROLE
Creating replication user replicator
ALTER ROLE
gis db already exists
                              List of databases
   Name    |   Owner   | Encoding | Collate |  Ctype  |   Access privileges   
-----------+-----------+----------+---------+---------+-----------------------
 gis       | gis_admin | UTF8     | C.UTF-8 | C.UTF-8 | 
 postgres  | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | 
 template0 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
 template1 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
(4 rows)

2020-02-08 13:50:24.785 UTC [25] LOG:  received smart shutdown request
2020-02-08 13:50:24.799 UTC [25] LOG:  background worker "logical replication launcher" (PID 58) exited with exit code 1
2020-02-08 13:50:24.801 UTC [53] LOG:  shutting down
2020-02-08 13:50:24.838 UTC [25] LOG:  database system is shut down
Postgres initialisation process completed .... restarting in foreground
2020-02-08 13:50:25.842 UTC [148] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-02-08 13:50:25.842 UTC [148] LOG:  listening on IPv6 address "::", port 5432
2020-02-08 13:50:25.850 UTC [148] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-02-08 13:50:25.880 UTC [150] LOG:  database system was shut down at 2020-02-08 13:50:24 UTC
2020-02-08 13:50:25.887 UTC [148] LOG:  database system is ready to accept connections

看起来postgre始于ip 0.0.0.0

我查看了dockerip addr show命令使用的IP 尝试使用此ip重新连接:

psql: could not connect to server: Connection refused
    Is the server running on host "172.17.0.1" and accepting
    TCP/IP connections on port 5432?
root@10fafbab73dc:/deploy/app# psql -h 172.17.255.255 -U gis_admin-gis
psql: could not connect to server: Connection timed out
    Is the server running on host "172.17.255.255" and accepting
    TCP/IP connections on port 5432?

我如何尝试将脚本连接到数据库?

Zeitounator

TLDR;

psql -h psql_postgis_db -U gis_admin gis
# or
psql -h psql_postgis_db gis gis_admin

服务器地址有问题

您尝试使用的IP均未正确。

  • 127.0.0.1是本地主机地址。由于您是从烧瓶容器启动命令的,因此那里没有posgres服务在运行。
  • 172.17.0.1是docker bridge的IP。实际上,这是您在同一桥上的容器所看到的docker引擎主机的ip。除非您的计算机上运行着postgres并收听该ip,否则您将无法获得任何答案(无论如何这都不是正确的postgres服务器)
  • 172.17.255.255是前一个网桥网络的网络广播地址。

从您的启动日志中,我们可以看到您的postgres应该正确监听。0.0.0.0实际上不是真正的IP:它代表“此主机上配置的任何IP”。

您可以寻找postgres容器的ip与之联系(例如,参见SO上的此答案),但是您甚至不必这样做。docker / docker-compose通过自动将同一网络上的容器/服务名称映射到其各自的IP,使您轻松实现这一点。因此,使用服务名称即可访问您的数据库服务器psql_postgis_db

用户和数据库名称有问题

我真的不明白您在命令-U选项中的内容psql(用户名+数据库名的混合...)。无论如何,它应该是您要用于连接到postgres服务器的用户名。在您的撰写文件中为gis_admin由于没有与用户同名的数据库,因此需要指定要连接的数据库名称。您可以使用-Uoption(psql -U <user> <db>)或位置参数(psql <db> <user>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我无法连接到AWS RDS?

无法连接到Docker中的Postgres服务器

为什么我无法连接到Gremlin-Server?

Django无法在Docker安装程序中连接到Postgres

为什么我无法连接到Selenium Server?

为什么Wordpress无法在此docker-compose应用中连接到MySQL?

为什么我的Django应用程序无法连接到docker内部的postgres

无法连接到Python Shell外部的Docker容器中的postgres

为什么我的rails容器未连接到postgres容器?

为什么我不能将容器连接到Docker网络?

为什么我无法连接到我的Linux实例?

为什么我无法连接到此docker compose服务?

无法连接到Docker中的Postgres

Docker Compose:无法连接到Postgres

为什么我无法连接到无线?

尝试在Rust中连接到Postgres时,为什么以下内容无法编译

为什么我无法连接到数据库?

为什么我无法连接到特定网站?

为什么我无法连接到Ubuntu One?

为什么我的 RestTemple 无法连接到服务器?

为什么我无法连接到 docker 中的 postgres?

为什么我无法连接到我的 Atlas 集群?

无法从 Flask 连接到 Postgres Docker 容器

无法从我的本地机器连接到 docker 容器中的 postgres 数据库

为什么我的 .NET Web API 应用程序在使用 docker-compose 时无法连接到 docker 上的 MySQL?

docker-compose:为什么我的 webapp 无法连接到 mysql?

为什么我无法在 PHP 中连接到我的数据库?

为什么我无法通过 MQTT 连接到 Google Cloud IOT Core 中的设备?

无法连接到 Docker 托管的 Postgres