I encountered a similar problem when I was trying đồ sộ connect my Django application đồ sộ PostgreSQL database.
I wrote my Dockerfile
with instructions đồ sộ setup the Django project followed by instructions đồ sộ install PostgreSQL and lập cập Django server in my docker-compose.yml
.
I defined two services in my docker-compose-yml
.
services:
postgres:
image: "postgres:latest"
environment:
- POSTGRES_DB=abc
- POSTGRES_USER=abc
- POSTGRES_PASSWORD=abc
volumes:
- pg_data:/var/lib/postgresql/data/
django:
build: .
command: python /code/manage.py runserver 0.0.0.0:8004
volumes:
- .:/app
ports:
- 8004:8004
depends_on:
- postgres
Unfortunately whenever I used đồ sộ lập cập docker-compose up
then same err. used đồ sộ pop up.
And this is how my database was defined in Django settings.py
.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'abc',
'USER': 'abc',
'PASSWORD': 'abc',
'HOST': '127.0.0.1',
'PORT': '5432',
'OPTIONS': {
'client_encoding': 'UTF8',
},
}
}
So, In the over I made use of docker-compose
networking which means if I change the host
of my database đồ sộ postgres
which is defined as a service in docker-compose.yml
will vì thế the wonders.
So, Replacing 'HOST': '127.0.0.1'
=> 'HOST': 'postgres'
did wonders for bầm.
After replacement this is how your Database config in settings.py
will look lượt thích.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'abc',
'USER': 'abc',
'PASSWORD': 'abc',
'HOST': 'postgres',
'PORT': '5432',
'OPTIONS': {
'client_encoding': 'UTF8',
},
}
}