Home > database >  How to fix problems in docker postgres connection?
How to fix problems in docker postgres connection?

Time:05-08

Tried to raise docker postres docker flask. Neither of them was raised! I read some guides and watch videos about postgres and flask in docker, but stil can't start project. I have 2 problems: Firstly, in the docker container for postgres i have the problem:"FATAL: role "root" does not exist". Secondly, related problem in flask container:sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused

There are my configurating files: 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}'

I will try myself to solve this problems by googling, but your professionall help really needed!

CodePudding user response:

First error is related with line

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

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

You try to connect to database as root, and this account may not exists Try to change it to "tzuser"

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

Second error is related with first.

  • Related