无法从Python连接到Docker Postgresql实例

时间:2018-01-30 13:29:19

标签: python postgresql docker psycopg2

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

if(isset($_POST['car_do_search']))
{
     $paramCarMake = isset($_POST['make']) ? sanitize_text_field($_POST['make']) : '';
     $validCarMake = esc_sql($paramCarMake); // For SQL queries only
     // TODO: Add data saving to database
}

现在尝试与Python连接:

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#

任何人都可以解释为什么我无法使用Python连接到PostgreSQL - 即使我使用相同的参数/参数来启用命令行上的成功连接(使用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? >>> ?)。

[[其他信息]]

根据@Itvhillo的建议,我尝试使用桌面应用程序连接到PG服务。我使用以下命令运行docker服务:

docker exec

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

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

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

An error occurred while establishing the connection:

Long Message:
The connection attempt failed.

Details:
   Type: org.postgresql.util.PSQLException
   SQL State: 08001

[[其他信息2]]

这是我的Dockerfile的尾端:

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

4 个答案:

答案 0 :(得分:7)

如果您正在运行

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

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

$ nc -zv localhost 5432 
Connection to localhost 5432 port [tcp/postgresql] succeeded!

在这种情况下,您应该能够使用:

进行连接
>>> psycopg2.connect("dbname='cookiebox' user='postgres' host='localhost' password='nunyabiznes'") 

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

$ nc -zv localhost 5432 
nc: connect to localhost port 5432 (tcp) failed: Connection refused

然后这意味着PostgreSQL没有监听,因此Dockerfile中出现了问题,您需要在Dockerfile上发布更多详细信息以进行诊断。

答案 1 :(得分:3)

似乎PostgreSQL由于某种原因无法绑定套接字来侦听TCP连接。但它仍会侦听容器内的默认UNIX套接字,因此您可以通过docker exec -it $CONTAINER_NAME psql连接到它。

答案 2 :(得分:0)

在postgresql.conf文件中更改 listen_addresses =&#39; localhost&#39; to listen_addresses =&#39; *&#39; 然后尝试通过psql连接

  

psql -h docker-ip -u username -w

答案 3 :(得分:0)

尝试隔离问题:在默认端口上运行clean postgres实例而不使用任何扩展名:

docker run  -d --name tst-postgres-01 -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword postgres:9.6

并尝试连接到它。如果可以,那么你必须相应地检查你的Dockerfile:从那里删除所有内容然后逐个添加新内容。否则,如果它没有连接,则尝试在其他端口上运行它:

docker run  -d --name tst-postgres-01 -p 45432:5432 -e POSTGRES_PASSWORD=mysecretpassword postgres:9.6

尝试连接。如果它这次工作,那么在主机上发出你的网络配置,不知何故5432端口被其他应用程序阻止或使用。