PostgreSQL in Django on Shared hosting

Use PostgreSQL Database in Django on Shared Hosting

A while ago, I wrote a post about deploying Django on shared hosting which used MySQL database. Using the PostgreSQL database over MySQL databases has many advantages which you may see here. But the advantage specific to shared hosting is that you can use a version of Django higher than 2.1 (Which you can’t do with MySQL database). In this tutorial, I am using version 3.0 of Django.

Before following the procedure, it is necessary to completely setup a Django app in CPanel. You can see the step by step procedure for that here. If you already have a Django app on CPanel that is using MySQL database, see this guide to migrate it to PostgreSQL database.

Create a PostgreSQL Database

One thing that is important to mention here is that many shared hosting packages only provide the MySQL database and do not include PostgreSQL databases. If your hosting has the PostgreSQL database, you will see it in your CPanel’s right Statistics Panel.

If your hosting does not have PostgreSQL databases or you don’t have a hosting, I recommend the Stellar Plus Plan of NameCheap shared hosting. This is what I am using in this tutorial.

Create a Database

Log in to your CPanel and go to PostgreSQL Databases

Select a name for a new database and click on Create Database

Create a User

In the Add New User section, create a new user by giving its username and password.

Add the User to the Database

Scroll to the Add User To Database section and add the newly created user to the newly created database.

Use the Database in Django

Install psycopg2

Go to Terminal in your CPanel (you can also do this step via SSH) and install the PostgreSQL adapter (binary distribution) for python. You have to install it by running the following command after you have entered the virtual environment:

pip install psycopg2-binary

Make changes to settings.py

Now, edit your settings.py and add the following DATABASES settings.

'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'database_name',
        'USER': 'database_username',
        'PASSWORD': 'database_password',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
  • Replace the database_name with the name of the database you created.
  • Replace the database_username with the username of the user you added to the database.
  • Replace the database_password with the password of the user you added to the database.

In my case, the settings look like this:

Make sure to use 127.0.0.1 instead of localhost in the HOST. I made the mistake of using the localhost and I was unable to connect to the database. The hosting support was helpful enough to take a look at my code and make a correction. This is what they mentioned:

Restart the Python app

Go to Setup Python App in CPanel and restart the python app.

2 thoughts on “Use PostgreSQL Database in Django on Shared Hosting”

  1. Very useful! Thanks for posting your support email about 127.0.0.1 instead of localhost and how a cPanel user doesn’t have the level of permissions to translate ‘localhost’. So many nuances to keep in mind probably all different across hosting providers.

    Reply

Leave a Comment