access host database django docker

  1. Install the required packages: pip install psycopg2

  2. 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', } }

  3. 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 ```

  1. Build and run the Docker containers: docker-compose up --build

  2. Apply Django migrations to create database tables: python manage.py migrate

  3. 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.