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

Now, edit your and add the following DATABASES settings.

'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'database_name',
        'USER': 'database_username',
        'PASSWORD': 'database_password',
        'HOST': '',
        '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 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.

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

  1. Very useful! Thanks for posting your support email about 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.

  2. Great tutorial!
    But, how do you access postgreSQL database from cPanel?
    When I go to PostgreSQL Databases page I see the name of my database and of the user, but there seems to be no way to access my database. In phpPgAdmin I see only Login Failed.

  3. My problem is a version error on Namecheap.

    django.db.utils.OperationalError: SSL error: wrong version number
    expected authentication request from server, but received S

  4. How did you get django to install? I’m doing basically what you did, setting up a django 3.0 project with postgresql on namecheap. Settings pass integrity checks, but when I run migrate it fails on a SQL alter-table statement containing COLLATE, which isn’t supported in postgre until v.9.1.

    • I will try my method again and make updates if necessary. You can contact me using the contact page of the website. I’d love to help you.

  5. Very Useful tip although I am experience an issue when i run makemigrations:
    django.db.utils.OperationalError: FATAL: no pg_hba.conf entry for host “::1”, user “db_username”, database “db_database_name”, SSL off,
    Any help

      • Yes I have. all my details are correct. But when run makemigrations I get that error. is there a way i can access postgresql.conf file or do do have any suggestions how i can resolve my issues. my django version is 3.1, python version is 3.8.1, and postgres version is 8.4.2


Leave a Comment