python - psycopg2.OperationalError : could not connect to server: Connection refused

标签 python postgresql docker docker-compose

import os
import datetime
from flask import Flask, render_template, redirect, url_for
# from database import DatabaseConnection
# from models import kayıt
from forms import LoginForm
import psycopg2 as p
        
conn=p.connect(dbname='app_db', user='postgres', host='localhost', password='samet', port=5432)
conn.autocommit=True
cur=conn.cursor()   

连接线

psycopg2.OperationalError: could not connect to server: Connection refused

使用; postgresql:9.6.5 python :3

如果我只用信息运行 postgre,但它没有在 python 中运行

谢谢

对不起,我应该解释得更清楚一点。我正在和一个 docker 打交道。我有 3 个容器,但我正在尝试通过 PostgreSQL 使用 python 容器。我想为此使用 psycopg2。但是我收到这样的错误。我的登录信息是正确的。我查过了。

最佳答案

想出如何解决这个问题:

根据 this answer :

Postgres is not running in the same container as the flask application, that why it cannot be acceded via localhost. we should find the IP address of the docker container with flask and add it, or just add Postgres or volume_name in place of localhost.

所以你的连接应该是这样的:

conn=p.connect(dbname='app_db', user='postgres', host='volume_name_of_postgres_in_docker_compose', password='samet', port=543

关于python - psycopg2.OperationalError : could not connect to server: Connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50130234/

相关文章:

python - 使用 Python 在 Google App Engine 中的应用启动时加载全局可访问的单例

database - Postgresql 复制 CSV 错误 : extra data after last expected column

postgresql - pgAdmin 执行脚本的快捷方式

docker - 使用 Docker Compose 启动多节点集群 - 在哪里配置主机实际名称?

python - 使用 alembic 创建数据库表和在 SQLAlchemy 中定义模型有什么区别?

python - 我想知道如何将带有值列表的字典映射到数据帧

asp.net-mvc - "upstream prematurely closed connection while reading response header from upstream"在 Ubuntu MVC4 应用程序上使用 nginx、PostgreSQL 和 Mono

docker - 如何在 PhpStorm 的 Docker 中打开一个项目?

wordpress - 更改文件权限在Wordpress dockerfile中不起作用

python - Bottle + nginx : connect() failed (111: Connection refused) while connecting to upstream