access host database django docker
Install the required packages:
pip install psycopg2
Update the
DATABASES
configuration in your Django settings file (settings.py
) to use the PostgreSQL database:python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'your_db_name', 'USER': 'your_db_user', 'PASSWORD': 'your_db_password', 'HOST': 'db', # Docker container name 'PORT': '5432', } }
Install Docker and create a
docker-compose.yml
file with PostgreSQL service: ```yaml version: '3'
services: db: image: postgres environment: POSTGRES_DB: your_db_name POSTGRES_USER: your_db_user POSTGRES_PASSWORD: your_db_password ```
Build and run the Docker containers:
docker-compose up --build
Apply Django migrations to create database tables:
python manage.py migrate
Run the Django development server:
python manage.py runserver
Now, your Django application should be connected to a PostgreSQL database running in a Docker container. Adjust the database configuration and Docker environment variables according to your specific needs.