Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use Docker-compose with Django to connect to a local instance of Postgresql, you need to follow these steps:

  1. Create a Dockerfile for Django and include psycopg2 in the requirements.txt file.

  2. Create a Docker-compose.yml file which will include the following services:

    • Django web service
    • Postgresql service
  3. In the Django settings.py file, set the database engine to django.db.backends.postgresql_psycopg2. You will also need to specify the database name, username, and password.

  4. In the Docker-compose.yml file, specify the database environment variables (POSTGRESDB, POSTGRESUSER, and POSTGRES_PASSWORD) for the Postgresql service.

  5. Map the Postgresql port to the local machine port in the Docker-compose.yml file.

  6. Run docker-compose up to start the services.

  7. In the Django application, update the database settings to connect to Postgresql on port 5432.

  8. Run the Django application to connect to the local instance of Postgresql.