0

I have "dockerized" a Django/PostgreSQL app and try to connect to my database I have 2 containers: web et db It works but I can't connect to my postgresql database

I used to ran docker exec -it coverage_africa_db_1 psql -U postgres but I got an error

psql: error: could not connect to server: FATAL: role "postgres" does not exist

I try to 'jump' into my container by running the command docker exec -it aab213f730cd bash and try to connect using psql command...

psql -d db_dev

psql: error: could not connect to server: FATAL: role "root" does not exist

or

psql -U postgres

error: could not connect to server: FATAL: role "postgres" does not exist

in fact, none of psql options works...

.env.dev

SECRET_KEY=*************************************
DEBUG=1
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=db_dev
SQL_USER=user
SQL_PASSWORD=user
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres
DJANGO_SETTINGS_MODULE=core.settings.dev

docker-compose.yml

version: '3.7'

services:
    web:
        build: ./app
        restart: always
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
            - ./app/:/usr/src/app
        ports:
            - 8000:8000
        env_file:
            - ./.env.dev
        depends_on: 
            - db
    db:
        image: postgres:12.0-alpine
        restart: always
        volumes:
            - postgres_data:/var/lib/postgres/data/
        environment:
            - POSTGRES_USER=user
            - POSTGRES_PASSWORD=user
            - POSTGRES_DB=db_dev
volumes:
    postgres_data:

1 Answer 1

1

With postgres container, this:

environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=user
- POSTGRES_DB=db_dev

defines how the database is initialized. If you didn't change it, you should be able to connect as user 'user' with password 'user'.

If you did change it, then the actual values are those which were present at the first launch. After first launch those credentials are written into the database, which data is on postgres_data volume. If you want to delete the data and reinitialize database with new credentials, use docker-compose down -v.

Sign up to request clarification or add additional context in comments.

6 Comments

thansk for replying. I did not change it and try docker exec -it coverage_africa_db_1 psql -U user and got an error psql: error: could not connect to server: FATAL: database "user" does not exist
don't understand why in one case it is role does not exist and other case database does not exist
I already do a docker-compose down (without -v) but did not change anything...
You named your database 'db_dev' with POSTGRES_DB=db_dev. Try docker exec -it coverage_africa_db_1 psql -d db_dev -U user.
You're welcome. Check this page if you want more on those variables hub.docker.com/_/postgres .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.