How to connect your django application to PostgreSQL using docker

Cover image

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

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