Been struggling with this issue for close to a month now and I am at my wits end.
I have the following docker-compose.yml file:
version: '3.8'
services:
web:
container_name: targeting
build: ./Targeting
command: python manage.py runserver 0.0.0.0:80
volumes:
- ./:/usr/src/project/
ports:
- "80:80"
db:
env_file:
- ./.env
restart: always
image: postgres
container_name: postgres
environment:
- POSTGRES_DB=${NAME}
- POSTGRES_USER=${USER}
- POSTGRES_PASSWORD=${PASSWORD}
- POSTGRES_PORT=${PORT}
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data/
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin4_container
restart: always
ports:
- "8888:80"
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: strong-password
volumes:
- pgadmin-data:/var/lib/pgadmin
volumes:
postgres_data:
pgadmin-data:
and I have the following .env file:
SECRET_KEY=random_secret_key
DEBUG=TRUE
NAME=postgres
USER=postgres
PASSWORD=password
HOST=db
PORT=5432
The following Dockerfile is used to create the python container:
# pull official base image
FROM python:3.11.4-slim-buster
# set work directory
WORKDIR /usr/src/project
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
RUN pip install --upgrade pip
COPY ../requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY .. .
On a Windows machine, after a docker compose build and a docker compose up command is issued, the project will be up and running and the django project will connect to the postgres database perfectly fine. I have verified this on two Windows machines. However, on mac or linux, I get a “connection failed: FATAL: password authentication failed for the user “postgres” error and I have no idea why.
my settings.py file for Django looks like this. I use Python decouple to use .env variables.
from decouple import config
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": config('NAME'),
"USER": config('USER'),
"PASSWORD": config('PASSWORD'),
"HOST": config('HOST'),
"PORT": config('PORT')
}
}
Anyone have any suggestions? I am guessing it could be something to do with paths, but I have not been able to find out the solution. Thanks.
I have tried credentials for a postgres database on a RDS instance in the env file and that worked fine on the Mac and linux machines.
1
I’ve run into this before if I made changes to the Postgres credentials and re-used the same volume. The Postgres volume will persist the initial POSTGRES_USER
and POSTGRES_PASSWORD
values from the first time compose was used to create the containers. I had changed one or both of those values, but didn’t destroy the old Postgres volume, and I would get similar user/password errors to your issue when Django tried to connect.
I run on WSL2 and my compose file looks similar to yours. I don’t think you’re missing anything platform-specific.
I would test a fresh anonymous volume for your postgres
service and see if it accepts the given credentials. I would also try to get into a PgAdmin shell and connect to the DB container with the credentials used by Django.