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.
Useful Links
- How to deploy a Django app on shared hosting
- Set up media files and static files in Django on shared hosting
- Migrate your Django app from MySQL to PostgreSQL database in Django on shared hosting.
- Buy NameCheap Stellar Plus hosting that has support for Django and provides unlimited PostgreSQL databases with unlimited bandwidth.
Very useful
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.
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.
Contact your hosting provider about this login failed issue. This should not happen.
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
In which step did you face the error? And please share the complete traceback.
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.
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.
I am on namecheap stellar plus plan
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
Have you set up the database correctly in settings.py ?
Yes I have. all my details are correct. But when run manage.py 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
i was able to fix it. the problem was my environment variable
Yes make sure there is no any trailing whitespace in the environment variable values
pls what exactly did you do
how they you resoloved please
Resolve what?
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.
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?
Umar, I was looking for a way to install a new version of Django on my shared hosting account. Fortunately, you helped me. A big thanks to you!
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.
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!