To use Docker-compose with Django to connect to a local instance of Postgresql, you need to follow these steps:
Create a Dockerfile for Django and include psycopg2
in the requirements.txt file.
Create a Docker-compose.yml file which will include the following services:
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.
In the Docker-compose.yml file, specify the database environment variables (POSTGRESDB, POSTGRESUSER, and POSTGRES_PASSWORD) for the Postgresql service.
Map the Postgresql port to the local machine port in the Docker-compose.yml file.
Run docker-compose up
to start the services.
In the Django application, update the database settings to connect to Postgresql on port 5432
.
Run the Django application to connect to the local instance of Postgresql.
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
Asked: 2023-02-13 11:00:00 +0000
Seen: 11 times
Last updated: Nov 17 '21
What is the procedure for using pg_restore on Windows with Docker?
Due to SyntaxError, why am I unable to create a TIMESTAMP WITH TIMEZONE column in postgres?
What are the benefits of choosing sqlalchemy.types instead of sqlalchemy.dialects.mssql?
What is the method for placing parentheses in column names when creating a table using an SQL query?
How can larger BLOBs be compressed without being inlined?
How can pgcrypto be used to secure data on Postgres?
How can you apply a filter using in_() in SQLAlchemy for JSON data?