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.

22 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
  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.

    Reply
  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

    Reply
  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 manage.py migrate it fails on a SQL alter-table statement containing COLLATE, which isn’t supported in postgre until v.9.1.

    Reply
    • 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.

      Reply
  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

    Reply
  6. wah, after 3 weeks of trying and bumping on errors, have been tried both postgre and mysql but none seemed to work for me. thanks so much for this.

    Reply
  7. i have a problem thats is shown :
    syntax error at or near “ON”
    LINE 1: …ory” (“seriesgo_id”, “category_id”) VALUES (4, 1) ON CONFLIC…
    how i can resolve thats?

    Reply
  8. Umar, I was looking to install a new version of Django on my shared hosting account. Fortunately, you helped me. A big thanks to you!

    But before that, I had encountered another error during the migration of the Django project.

    Reply
  9. Dear Umar, I was upset to install a new version of Django on my shared hosting account. Fortunately, you helped me. A big thanks to you!

    Reply

Leave a Comment