【问题标题】:PostgreSQL cannot connect to fastapi applicaiton via SQLalchemy utilizing docker-composePostgreSQL 无法使用 docker-compose 通过 SQLalchemy 连接到 fastapi 应用程序
【发布时间】:2020-02-29 07:22:53
【问题描述】:
test-api_1  | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
test-api_1  |   Is the server running on host "0.0.0.0" and accepting
test-api_1  |   TCP/IP connections on port 5432?
test-api_1  | 
test-api_1  | (Background on this error at: http://sqlalche.me/e/e3q8)
partnerup-api_test-api_1 exited with code 1

这是我得到的错误——它相对模糊,没有提供更多信息。

从 docker-compose cli 中,我得到以下信息:

my_postgres | 2019-11-03 23:05:21.746 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
my_postgres | 2019-11-03 23:05:21.746 UTC [1] LOG:  listening on IPv6 address "::", port 5432
my_postgres | 2019-11-03 23:05:21.758 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
my_postgres | 2019-11-03 23:05:21.817 UTC [51] LOG:  database system was shut down at 2019-11-03 23:05:21 UTC
my_postgres | 2019-11-03 23:05:21.847 UTC [1] LOG:  database system is ready to accept connections

这令人困惑,因为它似乎正在侦听,但基础 api 应用程序无法通过 sql alchemy 识别。

我尝试导航到 postgres.conf,但发现 listen_adress = '*'

除此之外,我不知道可能是什么问题。

数据库.py

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker


SQLALCHEMY_TRACK_MODIFICATIONS = False
## from sql alchemy docs --- format: postgresql://<user>:<password>@<host>:<port>/<database>
SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:dbpw@0.0.0.0:5432/partnerup'
engine = create_engine(SQLALCHEMY_DATABASE_URI)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()

docker-compose.yml

version: '3' #docker-compose version
services:       #containers in the network
  test-api:
    build: .
    ports:
      - 5000:5000 #port to go to to get a local version of the app
    volumes:
      - ./apps/api/app:/app
    working_dir: /app
    depends_on: 
      - db
    command:
      ['uvicorn', 'main:app', '--host', '127.0.0.1', '--port', '5000']
      #may integrate later
  db:
    image: "postgres:11"
    container_name: "my_postgres"
    restart: always 
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: 'postgres'
      POSTGRES_PASSWORD: 'dbpw'
      POSTGRES_DATABASE: 'partnerup'
      POSTGRES_HOST: 'localhost'
      POSTGRES_PORT: '5432'
    volumes:
          # this command will create database as described in init file 
     # - .:/docker-entrypoint-initdb.d/       
      - db_volume:/var/lib/postgresql
volumes:
  db_volume: {}

我希望我应该能够验证连接,但我不断收到令人讨厌的错误消息。

【问题讨论】:

  • 您的环境条目在 docker compose 中是否有效?我只见过- NAME=value 格式的它们。您可能还想尝试将 /data 附加到 postgres 卷安装路径
  • @Tomanow,是的,它们是有效的,我尝试切换它们只是为了好玩,但没有运气——不幸的是。我还按照您的建议对卷进行了更改。我不禁认为我对自己在做什么有某种根本性的误解。尽管如此,还是感谢您的反馈。

标签: python postgresql sqlalchemy docker-compose fastapi


【解决方案1】:

当使用 docker-compose 并尝试连接到数据库容器时,您需要连接到服务而不是指定,在我的实例中:

0.0.0.0:5432 

SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:dbpw@0.0.0.0:5432/partnerup'

相反,它应该像这样命名服务:

SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:dbpw@db/partnerup'

其中 db 是服务容器的名称。

还有一个可能面临的问题:

如果数据库容器没有完成旋转并且 api 服务尝试连接,也会遇到连接错误。解决此问题的一种方法是在 bash 代码中添加某种等待脚本,这可能是首选。

另一种不太干净的方法是像这样利用 docker-compose 重启功能。

在您的 api 服务中添加

restart: on-failure

告诉你 api 服务不断重启直到它成功。在我看来,这是一种让数据库服务有时间启动的丑陋方式。

****注:

depends_on: - db

在 test-api 服务中,确实会影响服务启动的顺序,但不会检查 db 服务的运行状况以确保它已完成启动。在某些情况下,这可能就是您需要做的所有事情。就我而言,这还不够,因为 db 容器需要一段时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    相关资源
    最近更新 更多