How to Connect PostgreSQL DB to Your Django Application

farhanghazi97
The Startup
Published in
3 min readAug 25, 2020

All Django applications come with an out-of-the-box SQLite database set up for your application. However, oftentimes, we prefer to use a different database backend that meets our requirements. In this article, we look at how we can connect a local PostgreSQL database to our Django application.

Before we proceed, we’re assuming that you have PostgreSQL set up on your machine. If you don’t have PostgreSQL set up, follow the link here to get started: https://www.postgresql.org/download/.

If all’s well, you should now have access to psql — the interactive command-line utility for accessing your PostgreSQL databases. If you want a UI-alternative, you can look into getting pgAdmin.

Open up your terminal and run the following: psql. You should then be prompted to run through some steps to configure PostgreSQL on your machine. Set up your user account and password (when prompted) and stick to the defaults (localhost , port etc) for the rest.

Once the set up process is complete, we can now proceed to create the local database for our Django application. It is best practise to set up a new user that will have access rights to the database we will create. As such, we’ll create a new user account first and give them the appropriate permissions. Within psql, do the following:

CREATE USER "app_user" WITH PASSWORD 'secret_password';

You should replace the username and password as per your requirement. With our user account set up, we can now proceed to create our database.

CREATE DATABASE "app_db";

You should name your database appropriately. With our user and database set up, let’s now give our user permission to manipulate this database.

GRANT ALL PRIVILEGES ON DATABASE "app_db" TO "app_user";

Awesome! We now have our database set up and are ready to connect it to our Django application. Time to write some backend code!

Assuming you have your model definitions set up and ready, we now simply need to update our settings.py and apply the necessary migrations.

settings.py# Proceed to alter the DATABASES dictionaryDATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'app_db',
'USER': 'app_user',
'PASSWORD': 'secret_password',
'HOST': 'localhost',
'PORT': '5432',
}
}

With our database definitions set up, all that’s left is to get Django to perform the migrations and set up the appropriate tables. Change directory into your project folder (where manage.py is) and do the following:

1) python manage.py makemigrations
2) python manage.py migrate

Running the above commands will import all our project-wide tables to our database (such as auth_user). Next, we’ll import our application-specific tables.

3) python manage.py makemigrations <app_name>
4) python manage.py migrate <app_name>
# where <app_name> is the name of the app you created using django- admin startapp

Running the above will import all our application models (defined within models.py) into our database.

Voila! We now have everything set up. You can proceed to inspect the actual database via psql and confirm if the migrations have been applied successfully. Your Django application will now perform all CRUD operations on this database and you have the added advantage of using psql to inspect it explicitly!

Feel free to let me know if this helped you. Thanks for reading!

--

--

farhanghazi97
The Startup

Just writing about stuff I got stuck with, so that you guys don’t have to struggle with the same things. Find out more: https://www.farhanghazi.xyz/