How to connecting PostgreSQL with a Django application
3 min
Ankaj Gupta
April 13, 2021

How to connecting PostgreSQL with a Django application

Connect Django App to PostgreSQL Database

In this article, you will learn how to connect a PostgreSQL database to a Django Python web application. You will also learn how to edit your Django Project settings file and connect your web application to a PostgreSQL database.

django connect with PostgreSQL

Introduction

Django is a flexible framework for quickly creating Python applications. By default, Django applications are configured to store data into a lightweight SQLite database file. While this works well under some loads, a more traditional DBMS like PostgreSQL can significantly improve performance in production.

Why PostgreSQL? PostgreSQL offers better performance, advanced features, and robust data integrity for production applications.

Video Tutorial

Step-by-Step Guide

Step 1: Create PostgreSQL Database

When you open the pgAdmin panel, you will view a server icon in the navigation at the left side of the window. Click on it to open the databases icon. Right-click on the databases icon to create a new database and fill the prerequisites for creating the database.

Step 2: Install psycopg2 Adapter

Install an adapter to connect the database with the Django app. We will be using psycopg2 for this purpose.

pip install psycopg2

Note: To install psycopg2 adapter, we also need to install Pillow if we wish to save any pictures in our database.

pip install pillow

Step 3: Configure Django Settings

Go to the settings.py file of your Django app. Under the settings file for databases, change the settings from SQLite or your current database to PostgreSQL.

Default SQLite Configuration:

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

PostgreSQL Configuration:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'your-db-name',
        'USER': 'user_name',
        'PASSWORD': 'your-db-password',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Important: Replace 'your-db-name', 'user_name', and 'your-db-password' with your actual PostgreSQL credentials.

Step 4: Run Makemigrations

Run the makemigrations command to convert all your models into database queries.

python manage.py makemigrations

Note: The correct command is python manage.py, not pip manage.py.

Step 5: Run Migrate

Finally, run the migrate command to migrate these changes into your PostgreSQL database.

python manage.py migrate

Verify Your Setup

You can verify the connection by checking your PostgreSQL database in pgAdmin:

Navigate to: Servers → PostgreSQL → Databases → DB_Name → Schema → Public → Tables

You should see Django's built-in tables created in your PostgreSQL database, confirming that the connection is successful!

Configuration Details

Setting Value Description
ENGINE django.db.backends.postgresql_psycopg2 Database backend adapter
NAME your-db-name Database name
USER user_name PostgreSQL username
PASSWORD your-db-password Database password
HOST localhost Database server hostname
PORT 5432 PostgreSQL default port

Benefits of Using PostgreSQL

Performance

Better performance for large-scale applications with concurrent users

Advanced Features

Support for complex queries, JSON data, and full-text search

Data Integrity

ACID compliance and robust data validation

Scalability

Handles large datasets and high traffic efficiently

Summary

Connecting Django to PostgreSQL is a straightforward process that involves installing the psycopg2 adapter, configuring your database settings in settings.py, and running migrations to create the necessary tables.

This setup provides a robust foundation for production Django applications, offering better performance, scalability, and advanced database features compared to the default SQLite database.

django PostgreSQL

Related Posts