如何解决 docker postgres 连接中的问题?

How to fix problems in docker postgres connection?

试图举起 docker postres + docker 烧瓶。两个都没养!我在 docker 中阅读了一些指南并观看了有关 postgres 和 flask 的视频,但仍然无法启动项目。 我有两个问题: 首先,在 postgres 的 docker 容器中,我遇到了问题:“FATAL:角色“root”不存在 ”。 其次,flask容器中的相关问题:sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused

这是我的配置文件: docker-compose.yaml

version: '3.9'
services:
  app:
    build:
      context: ./app
    ports:
      - 5000:5000
    volumes:
      - ./app:/app
  postgres:
    image: postgres:13.3
    healthcheck:
      test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
      timeout: 45s
      interval: 10s
      retries: 10
    restart: always
    environment:
      POSTGRES_DB: tzdb
      POSTGRES_USER: tzuser
      POSTGRES_PASSWORD: password
    ports:
      - 5432:5432
    volumes:
      # - ./db.sql:/docker-entrypoint-initdb.d/db.sql
      # - ./postgres-data:/var/lib/postgresql/data
      - ./db-data/:/var/lib/postgresql/data/
        # - ./init.sql:/docker-entrypoint-initdb.d/init.sql

Dockerfile:

FROM python:3.10

WORKDIR /app
COPY . /app

RUN pip install -r requirements.txt

ENTRYPOINT ["python"]
CMD ["app.py"]

config.py:

user = "tzuser"
password = "Vydik007123"
host = "postgres"
database = "tzdb"
port = "5432"

DATABASE_CONNECTION_URI = f'postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}'

我会尝试自己通过谷歌搜索来解决这个问题,但确实需要您的专业帮助!

第一个错误与行有关

test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]

https://www.postgresql.org/docs/current/app-pg-isready.html

您尝试以 root 身份连接到数据库,但此帐户可能不存在 尝试将其更改为“tzuser”

https://www.postgresql.org/docs/current/app-pg-isready.html

第二个错误与第一个有关。