I'm trying to connect my project with postgres database using docker.It works fine when I use default postgres setup like
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432
}
}
but when i try to create a database using postgre admin and want to use it in my django project it shows the error. my Dockerfile:
Dockerfile
# Pull base image
FROM python:3.7
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
# Copy project
COPY . /code/
the settings.py for databse
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'library',
'USER': 'postgres',
'PASSWORD': 'my_password',
'HOST': 'db',
'PORT': 5432
}
}
and the docker-compose.yml
docker-compose.yml
version: '3.7'
services:
db:
hostname: db
image: postgres:11
environment:
- POSTGRES_DB=library
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=my_password
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
volumes:
postgres_data: