Django-based Docker project will connect to local postgres DB on windows machines but not on a Mac or Linux machine

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật