admin管理员组

文章数量:1336593

It's my docker-compose.yaml

version: '3.3'

services:
  django:
    build: ./project # path to Dockerfile
    command: sh -c " 
      sleep 3 && gunicorn --bind 0.0.0.0:8000 core_app.wsgi"
    volumes:
      - ./project:/project
      - ./project/static:/project/static
      - ./project/media:/project/media
      - ./project/logs:/project/logs
    expose:
      - 8000
    env_file:
      - ./.env
    depends_on:
      - db

  
  db:
    image: postgres:13-alpine
    volumes:
      - pg_data:/var/lib/postgresql/data/
    expose: 
      - 5432
    ports:
      - "5433:5432"
    env_file:
      - .env
    environment:
      - POSTGRES_USER=postgresuser
      - POSTGRES_PASSWORD=post222
      - POSTGRES_DB=yandex_parser_db

I can't connect to the postgres db inside db docker container My actions:

sudo docker-compose ps
               Name                              Command               State            Ports         
------------------------------------------------------------------------------------------------------
app-content-protect_db_1              docker-entrypoint.sh postgres    Up       0.0.0.0:5433->5432/tcp
app-content-protect_django_1          sh -c  sleep 3 && gunicorn ...   Exit 3         


sudo docker exec -ti app-content-protect_db_1 bash
e21385ac10e4:/# psql -h db -U postgresuser
Password for user postgresuser: 
psql: error: FATAL:  password authentication failed for user "postgresuser"

Also I was trying to run docker-compose and have simillar error

db_1             | 2024-11-19 17:59:52.314 UTC [32] FATAL:  password authentication failed for user "postgresuser"
db_1             | 2024-11-19 17:59:52.314 UTC [32] DETAIL:  Role "postgresuser" does not exist.
db_1             |  Connection matched pg_hba.conf line 99: "host all all all md5"
django_1         | [2024-11-19 20:59:52 +0300] [9] [ERROR] Exception in worker process
django_1         | Traceback (most recent call last):
django_1         |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
django_1         |     self.connect()
django_1         |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
django_1         |     return func(*args, **kwargs)
django_1         |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
django_1         |     self.connection = self.get_new_connection(conn_params)
django_1         |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
django_1         |     return func(*args, **kwargs)
django_1         |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
django_1         |     connection = Database.connect(**conn_params)
django_1         |   File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
django_1         |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django_1         | psycopg2.OperationalError: FATAL:  password authentication failed for user "postgresuser"

It's my docker-compose.yaml

version: '3.3'

services:
  django:
    build: ./project # path to Dockerfile
    command: sh -c " 
      sleep 3 && gunicorn --bind 0.0.0.0:8000 core_app.wsgi"
    volumes:
      - ./project:/project
      - ./project/static:/project/static
      - ./project/media:/project/media
      - ./project/logs:/project/logs
    expose:
      - 8000
    env_file:
      - ./.env
    depends_on:
      - db

  
  db:
    image: postgres:13-alpine
    volumes:
      - pg_data:/var/lib/postgresql/data/
    expose: 
      - 5432
    ports:
      - "5433:5432"
    env_file:
      - .env
    environment:
      - POSTGRES_USER=postgresuser
      - POSTGRES_PASSWORD=post222
      - POSTGRES_DB=yandex_parser_db

I can't connect to the postgres db inside db docker container My actions:

sudo docker-compose ps
               Name                              Command               State            Ports         
------------------------------------------------------------------------------------------------------
app-content-protect_db_1              docker-entrypoint.sh postgres    Up       0.0.0.0:5433->5432/tcp
app-content-protect_django_1          sh -c  sleep 3 && gunicorn ...   Exit 3         


sudo docker exec -ti app-content-protect_db_1 bash
e21385ac10e4:/# psql -h db -U postgresuser
Password for user postgresuser: 
psql: error: FATAL:  password authentication failed for user "postgresuser"

Also I was trying to run docker-compose and have simillar error

db_1             | 2024-11-19 17:59:52.314 UTC [32] FATAL:  password authentication failed for user "postgresuser"
db_1             | 2024-11-19 17:59:52.314 UTC [32] DETAIL:  Role "postgresuser" does not exist.
db_1             |  Connection matched pg_hba.conf line 99: "host all all all md5"
django_1         | [2024-11-19 20:59:52 +0300] [9] [ERROR] Exception in worker process
django_1         | Traceback (most recent call last):
django_1         |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
django_1         |     self.connect()
django_1         |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
django_1         |     return func(*args, **kwargs)
django_1         |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
django_1         |     self.connection = self.get_new_connection(conn_params)
django_1         |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
django_1         |     return func(*args, **kwargs)
django_1         |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
django_1         |     connection = Database.connect(**conn_params)
django_1         |   File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
django_1         |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django_1         | psycopg2.OperationalError: FATAL:  password authentication failed for user "postgresuser"
Share Improve this question asked Nov 19, 2024 at 18:15 mascaimascai 1,8822 gold badges18 silver badges48 bronze badges 1
  • 1 Have you changed the database's environment: settings after creating the database once? docker-compose down -v will delete the entire database state, but if you can do that, does it help? – David Maze Commented Nov 19, 2024 at 18:55
Add a comment  | 

1 Answer 1

Reset to default 0

I updated the docker compose file, ran locally and could connect to DB.

This is the update file:

services:
    django:
        container_name: myapp_app
        build: ./project # path to Dockerfile
        command: sh -c "
            sleep 3 && gunicorn --bind 0.0.0.0:8000 core_app.wsgi"
        volumes:
            - ./project:/project
            - ./project/static:/project/static
            - ./project/media:/project/media
            - ./project/logs:/project/logs
        expose:
            - 8000
        env_file:
            - ./.env
        depends_on:
            - db
        networks:
            - myapp


    db:
        container_name: myapp_psql
        image: postgres:13-alpine
        volumes:
            - pg_data:/var/lib/postgresql/data/
        ports:
            - "5433:5432"
        environment:
            - POSTGRES_USER=postgresuser
            - POSTGRES_PASSWORD=post222
            - POSTGRES_DB=yandex_parser_db
        networks:
            - myapp

networks:
    myapp:
        driver: bridge

Changes made:

  1. Removed the version of docker compose (you probably had a warning)
  2. Removed exposed port in db service, it's useless
  3. Removed the .env file from db service, there should be only 3 env vars for postgres: user, pass and db
  4. Added network and connected all containers together
  5. Gave a name to all containers

本文标签: postgresqlDockercompose can39t connect to postgresStack Overflow