How to use PostgreSQL Database in Django?
Last Updated :
21 Feb, 2023
This article revolves around how can you change your default Django SQLite-server to PostgreSQL. PostgreSQL and SQLite are the most widely used RDBMS relational database management systems. They are both open-source and free. There are some major differences that you should be consider when you are choosing a database for your applications.
Also checkout - Difference between SQLite and PostgreSQL
Setting up PostgreSQL in Django
First create a virtual env so to do that first install virtualenv using this command
pip install virtualenv
then we will create a virtualenv named gfg using
virtualenv gfg
to enter in the virtual environment create use

now we will install Django here so I am using Django 2.2
pip install django==2.2.*
To get Python working with Postgres, you will need to install the “psycopg2” module.
pip install psycopg2
now lets create a django project named geeks
django-admin startproject geeks
to check your django is running smoothly
python manage.py runserver

Now, go to the below link and download and set up PostgreSQL. create a database name gfg in your Postgres server. Now its time to switch from SQLite to PostgreSQL.
Folder structure -

open the settings.py file
now change database settings with this template code
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': ‘<database_name>’,
'USER': '<database_username>',
'PASSWORD': '<password>',
'HOST': '<database_hostname_or_ip>',
'PORT': '<database_port>',
}
}
Run these commands
python manage.py makemigrations
python manage.py migrate

now lets create the default superuser:
python manage.py createsuperuser

now again run your server with
python manage.py runserver
go to this route and add the credential you did while creating superuser
http://127.0.0.1:8000/admin/

and if you are successfully able to log in, you have successfully switched to PostgreSQL

Similar Reads
Django Tutorial | Learn Django Framework Django is a Python framework that simplifies web development by handling complex tasks for you. It follows the "Don't Repeat Yourself" (DRY) principle, promoting reusable components and making development faster. With built-in features like user authentication, database connections, and CRUD operati
10 min read
PostgreSQL Tutorial In this PostgreSQL tutorial youâll learn the basic data types(Boolean, char, text, time, int etc.), Querying and Filtering techniques like select, where, in, order by, etc. managing and modifying the tables in PostgreSQL. Weâll cover all the basic to advance concepts of PostgreSQL in this tutorial.
8 min read
Python Web Development With Django Python Django is a web framework that allows to quickly create efficient web pages. Django is also called batteries included framework because it provides built-in features such as Django Admin Interface, default database - SQLite3, etc. When youâre building a website, you always need a similar set
15+ min read
PostgreSQL DATEDIFF Function PostgreSQL doesnât have a DATEDIFF function like some other databases, but you can still calculate the difference between dates using simple subtraction. This approach allows you to find out how many days, months, or years separate two dates. In this article, we'll explore how to compute date differ
6 min read
Python Django Projects with Source Code (Beginners to Advanced) Python Django Projects with Source Code - Adding a project portfolio to your resume helps to show your skills and potential to your recruiter. Because in the tech space, real-time project experience matters a lot. Now, to make your resume powerful, we have come up with the top Django projects with s
5 min read
Getting started with Jinja Template This article introduces Jinja and then describes the basics of working with it which includes how to render templates using Jinja and Python3, passing data to the template through arguments, and basic Jinja syntax (delimiters, expressions, control structures - decisions and loops). A few examples in
8 min read
Django Project MVT Structure Django follows the MVT (Model-View-Template) architectural pattern, which is a variation of the traditional MVC (Model-View-Controller) design pattern used in web development. This pattern separates the application into three main components:1. ModelModel acts as the data layer of your application.
2 min read
PostgreSQL - Data Types PostgreSQL is a powerful, open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column. which allows developers to define, store, and manipulate data in a way that aligns w
5 min read
PostgreSQL - Psql commands PostgreSQL, or Postgres, is an object-relational database management system that utilizes the SQL language. PSQL is a powerful interactive terminal for working with the PostgreSQL database. It enables users to execute queries efficiently and manage databases effectively.Here, we highlight some of th
2 min read
Django Installation and Setup Installing and setting up Django is a straightforward process. Below are the step-by-step instructions to install Django and set up a new Django project on your system.Prerequisites: Before installing Django, make sure you have Python installed on your system. How to Install Django?To Install Django
2 min read