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/