The Django framework comes into the picture when building web applications with Python. Django applications are set by default to store data in a lightweight SQLite database file. Although Django has an SQLite3 database by default, developers prefer PostgreSQL over SQLite3 database files.

The main reason for this is PostgreSQL’s customized offering to tackle complex databases Django PostgreSQL integration can increase production performance. If you are looking for steps to configure PostgreSQL Database with Django, then you landed in the right place. This guide will teach you how to connect PostgreSQL database in Django to use in your applications.

How to Establish Django PostgreSQL Connection?

Now that you have a clear vision of the benefits in your mind, let’s walk past the steps required to establish the Django PostgreSQL connection. But first, let’s state some prerequisites.

Step 1: Creating and Activating Virtual Environment

Open your Command Line Interface(CLI) and create a project directory using the following command. 

mkdir django-postgres

After creating the directory, the next step is to point towards the created directory with the following command.

cd django-postgres

In the Project development phase creating a virtual environment is a good practice. You can control the environment by segregating its environment from the rest of the systems.

The command to create a virtual environment is as follows:

python -m venv env;

The final step is to activate the virtual environment by running the command given below.

Make PostgreSQL Data Replication Seamless — Try Hevo Data Pipelines!

Are you looking for an easy way to replicate your PostgreSQL data? Hevo is the only real-time ELT No-code Data Pipeline platform that cost-effectively automates data pipelines that are flexible to your needs. With integration with 150+ Data Sources (40+ free sources), we help you not only export data from sources & load data to the destinations but also transform & enrich your data, & make it analysis-ready.

Start for free now!

Get Started with Hevo for Free

Step 2: Installing Django and PostgreSQL

Now you have created a virtual environment for your project, so it’s time to install the required dependencies i.e., Django and PostgreSQL.

pip install django

The above command will install the latest version of Django to your project. 

Install the “psycopg2” module to get Python to operate with Postgres.

pip install psycopg2

Step 3: Creating Application in Django

Let’s create our first project, which will only have one project in our project directory but will let us add as many applications as we need.

django-admin startproject postgresTest

As a result of the above command, the “postgreTest” project folder will be generated in your project directory. You can verify the same in Visual Studio Editor.

Django PostgreSQL - Django Project Directory | Hevo Data
Verify Project in Visual Studio Editor

Open up the project directory in Visual Studio and activate the virtual environment. Open the Visual Studio terminal and move to the project directory using the following command.

cd postgresTest
python startapp testdb

To launch your first app, use the following command.

Django PostgreSQL - Django Installed App | Hevo Data
Launch Your App

Note: Don’t forget to mention your newly created application in the INSTALLED_APPS list in the file.

Kudos to you for creating your first application in Django!

Now, try running the server to ensure that everything is working correctly. Run the following command in the visual studio terminal.

python runserver

If everything is working correctly, you will get a link where your application is hosted on the local server. After clicking that link, you will land on the webpage shown below.

Django PostgreSQL - Django Application | Hevo Data
LInk Where Application is Hosted

Step 4: Configuring Django Application Settings

Create a database named “test” in your Postgres server using pgAdmin4. 

It is now time to switch from the default database connection, i.e., SQLite3, to PostgreSQL in your Django project.

  • Go to the file. 

By default, you will find the lines of code that are shown below. This code is configuring Django with the default database(SQLite3).

   'default': {
       'ENGINE': 'django.db.backends.sqlite3',
       'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

You need to change the above settings to the parameters as shown below:

   'default': {
       'ENGINE': 'django.db.backends.postgresql',
       'NAME': ‘<database_name>’,
       'USER': '<database_username>',
       'PASSWORD': '<password>',
       'HOST': '<database_hostname_or_ip>',
       'PORT': '<database_port>',

Step 5: Testing Connection 

Run the following command to migrate all tables in our Django project to PostgreSQL.

python makemigrations
python migrate

You will get the output as shown below.

Django PostgreSQL - Django Migration Command | Hevo Data

Congratulations!! You’ve successfully established the Django PostgreSQL connection.

Why is Django PostgreSQL Connection Useful?

The primary reasons to use Django PostgreSQL integration are mentioned below:

  • Django includes a variety of data types that are exclusively compatible with PostgreSQL.
  • Django provides django.contrib.postgres for PostgreSQL database operations.
  • If you are creating a map-based application or storing geographical data, you must utilize PostgreSQL since it works with GeoDjango.
  • Django supports the most functionality in PostgreSQL.
  • Django offers crucial PostgreSQL-specific features such as aggregation functions, database constraints, form fields widgets, lookups, full-text search, Validators, and more.

We’ve seen how Django is the most capable web framework, and PostgreSQL is the most robust and dependable RDBMS. As a developer, you can leverage the versatility of PostgreSQL to work with almost any type of data and the unbeatable features of Django to develop fast and secure web applications.


In this guide, you have learned about PostgreSQL and Django briefly, their key features respectively, benefits of establishing a Django PostgreSQL connection. Moreover, you now have a detailed understanding of how to use PostgreSQL with your Django application.

Hopefully, you will try leveraging the Django PostgreSQL connection for your future web applications. Feel free to leave a comment below expressing your thoughts or recommendations.

If you use PostgreSQL, copying data into a warehouse using ETL scripts can be time-consuming. And the fact that organizations require significant funds to recruit data engineers to maintain those scripts only exaggerates the problem. Here is where Hevo Data can help you.

With integration with 150+ Data Sources (40+ free sources), Hevo helps you not only export data from sources & load data to the destinations but also transform & enrich your data, & make it analysis-ready.

Visit our Website to Explore Hevo

Give Hevo a try. Sign Up here for a 14-day full feature access trial and experience the feature-rich Hevo suite firsthand. You can also check our unbeatable pricing and make a decision on your best-suited plan. 

Share your thoughts on learning about Django PostgreSQL in the comments section below. If you have any questions, do let us know. We’d be happy to help.

Former Marketing Analyst, Hevo Data

Kamya is a data science enthusiast who loves writing content to help data practitioners solve challenges associated with data integration. He has a flair for writing in-depth articles on data science.

No-code Data Pipeline For PostgreSQL