Ask Your Question
1

How can Docker-compose be used in Django to connect to a local instance of Postgresql?

asked 2023-02-13 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-11-17 21:00:00 +0000

lalupa gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-02-13 11:00:00 +0000

Seen: 11 times

Last updated: Nov 17 '21