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
How to integrate Mysql database with Django? Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database â SQLlite3, etc. Installation Let's first un
2 min read
How to Run Django's Test Using In-Memory Database By default, Django creates a test database in the file system, but it's possible to run tests using an in-memory database, which can make our tests faster because it avoids disk I/O operations.In this article, weâll explore how to run Django tests with an in-memory database, the advantages of this a
6 min read
PostgreSQL - Loading a Database In this article we will look into the process of loading a PostgreSQL database into the PostgreSQL database server. Before moving forward we just need to make sure of two things: PostgreSQL database server is installed on your system. A sample database. For the purpose of this article, we will be us
3 min read
How to create superuser in Django? Django comes with a built-in admin panel that allows developers to manage the database, users and other models efficiently. This eliminates the need for creating a separate admin interface from scratch. To access and manage the Django Admin panel, we need to create a superuser i.e. a user with full
2 min read
How to Create a Basic Project using MVT in Django ? Prerequisite - Django Project MVT Structure Assuming you have gone through the previous article. This article focuses on creating a basic project to render a template using MVT architecture. We will use MVT (Models, Views, Templates) to render data to a local server. Create a basic Project: To in
2 min read
How to Create Tables on Heroku Postgresql Heroku is a Platform as a Service (PaaS) used by developers to deploy their projects with different other requirements provided by the platform like database and all, whereas PostgreSQL is an open-source object-relational database system used for database management in many projects. This article is
4 min read