Reputation: 304
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:
Upvotes: 1
Views: 1883
Reputation: 32294
When the postgres image is first started and has no data it will create a database with the name of the POSTGRES_DB
environment variable.
You already have a database created in your volume. You should delete your postgres_data
volume and restart so that a database with the new name is created. Pass -v
to docker-compose down
to delete any volumes
docker-compose down -v
docker-compose up -d
Upvotes: 1