How to connect your django application to PostgreSQL using docker
In this tutorial we are going to learn how to connect Django application to a PostgreSQL database using Docker container while developing the django application. To get started we are going to follow the steps below:
- Step 1 - Create a PostgreSQL database using docker
- Step 2 - Install pgAdmin
- Step 3 - Create a django project
- Step 4 - Configure postgreSQL to django
- Step 5 - Run the django application
- Step 6 - Create a simple model
- Step 7 - Migrate the schema
- Step 8 - Run the migrations
- Step 9 - See the changes in the database
- Step 10 - Run the django application
- Step 11 - Deploy the django application to Heroku
Step 1 - Create a PostgreSQL database using docker
First we're goning to create a PostgreSQL database using docker. You can also download the postgreSQL binary from here and use it to create a PostgreSQL database.
But in our case we are going to use a docker container to create a PostgreSQL database. To create the PostgreSQL database we are going to use the following docker command:
docker run --name postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -d postgres:10-alpine
It will download the latest version of PostgreSQL 10 and create a database named postgres and a user named postgres with password postgres and run it in detached mode. If you want to it in interace mode you can run it like this:
docker run --name postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -i -t postgres:10-alpine