How to Connect PostgreSQL With Django Project

django postgres image
Reading Time: 4 minutes

Hi, Reader in this blog we are going to see How to Connect PostgreSQL With Django Project. I will perform these tasks in a virtual environment.

What is Virtualenv?

A Virtualenv is a way that you can separate different python environments for different projects. So why do you want to do something like this for example say that you have multiple projects and they all rely on a single package say it’s a flask or Django or something like that each one of these projects may be using a different version of Django or different version of flask now if you go and upgrade package then it could break a couple of your websites that might not be what you want to do it. It would be better if each of these projects has an isolated environment where they had only the dependencies and the packages they need, the specific version. That’s what virtual env allows us to do.

So first I will create a virtual environment using virtual env. If your machine does not have virtualenv first install virtualenv using the below command:

$ pip install virtualenv

After that I will create a virtual environment using the below command:

$ python3 -m virtualenv myenv1

After the successful creation of a virtual environment, you just need to activate your virtual environment so that you can work on that virtual environment. To activate just run this command:

$ source myenv/bin/activate

Install Django

Now you have to install Django in your virtual environment.

$ pip install django==version

In the above image you can see that first I have activated my virtual environment you can verify from the right-hand side and after that, I have installed Django’s latest version in the same virtual environment.

To connect to the PostgreSQL database server in the Django Project, use the psycopg2 database adapter. Install psycopg2 adapter in the same virtual environment as well. To install run the below command:

$ pip install django psycopg2 

Create a Django Project

Now first create a Django project. Here is how to create a Django project just run the below command:

$ django-admin startproject postgresProject

Configure the Django Database Settings

Now it’s time to connect the Django project with our PostgreSQL Database. To connect go to the setting.py file in the project directory. You will see a Database section in this file where you have to configure your PostgresSQL.In the below image, you can see that I am using os module to get all the environment variables.

To get all the environment variables first export the below variable in the .bashrc file.

DB_HOST=<db_hostname>
DB_NAME=<db_name>
DB_USER=<db_username>
DB_PASS=<db_password>

Creating a Table

Now we can run a migration but before going forward I will create a Django application in the same project and from that application, I will create a table in PostgreSQL Database. So first create a Django application from the below command:

$ python manage.py startapp myapp

After successfully creating your application go to the models.py file and define your table accordingly. Below I have attached an image in which I have defined a simple table with three columns.

After updating models.py you have to register your application into your project’s setting.py under the INTSALLED_APPS section.

Migrate the table to the PostgreSQL database

Now, we have successfully written our table but the table isn’t being sent to PostgreSQL yet. So what we must do are the following procedures.

  • makemigrations
  • migrate
$ python manage.py makemigrations

After that,

$ python manage.py migrate

Now we have successfully created our PostgreSQL database, and migrate all tables in our Django project to PostgreSQL.You can see in below image that table my_test_table has been created.

Refrences:

https://docs.djangoproject.com/en/4.0/ref/databases/

Conclusion

In this we have learned how to create virtual envrionment,how to create a Django project and application and after all we learned how to connect Django project with Postgresql.I hope you enjoyed this blog , please share your comment for your valuable feedback.Thank You!!!!

Written by 

Jubair Ahmad is a Software Consultant (DevOps)at Knoldus.Inc.He loves learning new technology and also have interest in playing cricket.