首页 > 解决方案 > sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)无法连接到服务器:连接被拒绝

问题描述

在 docker 上启动 postgres 后,任何连接到数据库的尝试都会导致以下结果;

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

此错误的背景:http ://sqlalche.me/e/e3q8

我已经研究过类似的问题,并且我的端口是这样分配的;

ports:
 - 5432:5432

细节

额外细节

Dockerfile(用于 postgres)

FROM postgres:9.6-alpine

COPY init-db /docker-entrypoint-initdb.d/

应用程序文件(FLASK_APP)这是破坏它的命令

@application.cli.command(with_appcontext=True)
def create_db():
    db.engine.execute("CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;")

    db.drop_all()
    db.create_all()

容器 ID 图像命令创建的状态端口名称 f04ca2725c96 taxai_ml "gunicorn --worker-c..." 3 小时前 3 小时前 0.0.0.0:8900->5000/tcp taxai_ml_1 032fd31432af stripemock/stripe-mock "/stripe-mock -http- ...” 3 小时前 3 小时前 0.0.0.0:12111-12112->12111-12112/tcp taxai_stripe_1 795b2ff4d7d5 redis:3.2.11-alpine "docker-entrypoint.s..." 3 小时前 3 小时前 0.0.0.0:6379 ->6379/tcp taxai_redis_1

标签: postgresqldocker

解决方案


推荐阅读