Docker Community Forums

Share and learn in the Docker community.

Integrate Django with a Postgres Docker container

docker

(Byakodpramod) #1

I have a situation where I need to connect my Django project to an existing Docker Postgres container. In simple words, when I execute python manage.py dbshell, django should connect to an existing Docker Postgres container. Currently my django is connected to sqlite3 db. Below is my settings.py snippet for connecting to db.

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,
‘NAME’: ‘etag_auth’,
‘USER’: ‘etag_master’,
‘PASSWORD’: ‘thisismypass’,
‘HOST’: ‘cybercom_postgres’,
‘PORT’: ‘5432’
#‘ENGINE’: ‘django.db.backends.sqlite3’,
#‘NAME’: os.path.join(BASE_DIR, ‘db.sqlite3’),
}
‘etag’: {
‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,
‘NAME’: ‘etag’,
‘USER’: ‘etag_master’,
‘PASSWORD’: ‘thisismypass’,
‘HOST’: ‘cybercom_postgres’,
‘PORT’: ‘5432’,
}
}

I also tried with docker-compose, but i am not able to make docker-compose to connect to existing container. Below is my docker-compose.yml code snippet

postgres:
external:
name: etag_postgres
image: postgres
ports:
- "5432:5432"
volumes:
- /data/db

Kindly help to solve the issue.