无法从Python连接到Docker Postgresql实例

Hom*_*lli 14 python postgresql psycopg2 docker

我正在使用Docker来"容纳"PostgreSQL部署.我可以通过命令行启动容器并连接到PostgreSQL,如下所示:

minime2@CEBERUS:~/Projects/skunkworks$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
dc176901052a        df:pg               "docker-entrypoint..."   About an hour ago   Up About an hour    5432/tcp            vigilant_agnesi

minime2@CEBERUS:~/Projects/skunkworks$ CONTAINER_ID=dc176901052a
minime2@CEBERUS:~/Projects/skunkworks$ IP=$(docker inspect -f '{{.NetworkSettings.Networks.bridge.IPAddress}}' $CONTAINER_ID)

minime2@CEBERUS:~/Projects/skunkworks$ echo $IP
172.17.0.2


minime2@CEBERUS:~/Projects/skunkworks$ docker exec -it vigilant_agnesi psql -U postgres -W cookiebox
Passwod for user postgres:
psql (9.6.5)
Type "help" for help

cookiebox#
Run Code Online (Sandbox Code Playgroud)

现在尝试与Python连接:

Python 3.5.2 (default, Sep 14 2017, 22:51:06) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> conn = psycopg2.connect("dbname='cookiebox' user='postgres' host='172.17.0.2' password='nunyabiznes'")                                     Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/minime2/Projects/skunkworks/archivers/env/lib/python3.5/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
        Is the server running on host "172.17.0.2" and accepting
        TCP/IP connections on port 5432?

>>> 
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么我无法使用Python连接到PostgreSQL - 即使我使用相同的参数/参数来启用命令行上的成功连接(使用docker exec?).

[[附加信息]]

正如@Itvhillo所建议的那样,我尝试使用桌面应用程序连接到PG服务.我使用以下命令运行docker服务:

docker run -i -p 5432:5432 --name $CONTAINER_NAME $DOCKER_IMAGE

我正在使用Db Visualizer连接到数据库,我已将主机名设置为"localhost".我可以成功ping通端口,但在尝试连接数据库时仍然收到错误消息(可能的权限相关错误):

An error occurred while establishing the connection:

Long Message:
The connection attempt failed.

Details:
   Type: org.postgresql.util.PSQLException
   SQL State: 08001
Run Code Online (Sandbox Code Playgroud)

顺便说一下,这是PG服务实例的输出的尾端:

PostgreSQL init process complete; ready for start up.

LOG:  could not bind IPv6 socket: Cannot assign requested address
HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG:  database system was shut down at 2018-01-30 16:21:59 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
Run Code Online (Sandbox Code Playgroud)

[[附加信息2]]

这是我的Dockerfile的尾端:

# modified target locations (checked by login onto Docker container)
# show hba_file;
# show config_file;

#################################################################################
# From here: https://docs.docker.com/engine/examples/postgresql_service/
# Adjust PostgreSQL configuration so that remote connections to the
# database are possible.
RUN echo "host all  all    0.0.0.0/0  md5" >> /var/lib/postgresql/data/pg_hba.conf

# And add ``listen_addresses`` to ``/var/lib/postgresql/data/postgresql.conf``
RUN echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf
#################################################################################


EXPOSE 5432

# Add VOLUMEs to allow backup of config, logs and databases
VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql", "/usr/lib/postgresql/"]
Run Code Online (Sandbox Code Playgroud)

Ste*_*ini 8

如果你在跑

$ docker run -i -p 5432:5432 --name $CONTAINER_NAME $DOCKER_IMAGE
Run Code Online (Sandbox Code Playgroud)

然后你应该能够localhost:5432从主机连接到.检查是否正在侦听端口5432的最简单方法是使用netcat.如果成功,你应该得到:

$ nc -zv localhost 5432 
Connection to localhost 5432 port [tcp/postgresql] succeeded!
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您应该能够使用以下方式进行连接:

>>> psycopg2.connect("dbname='cookiebox' user='postgres' host='localhost' password='nunyabiznes'") 
Run Code Online (Sandbox Code Playgroud)

另一方面,如果你得到类似的东西:

$ nc -zv localhost 5432 
nc: connect to localhost port 5432 (tcp) failed: Connection refused
Run Code Online (Sandbox Code Playgroud)

那么这意味着PostgreSQL没有监听,因此你的Dockerfile出了问题,你需要在Dockerfile上发布更多细节来诊断它.