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!
Hello.
Is the Postgres Version on Namecheap still working? I am aware version 8 is deprecated and currently not receiving support. I need to get started on a cheap Django & Postgres package.
Hello, i am getting the bellow error when i tried install psycopg2-binary :
“Building wheel for psycopg2-binary (setup.py) … error
error: subprocess-exited-with-error
× python setup.py bdist_wheel did not run successfully.
│ exit code: 1
╰─> [40 lines of output]
/home/josbmusc/virtualenv/josbreed_shop/3.9/lib/python3.9/site-packages/setuptools/config/setupcfg.py:508: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
warnings.warn(msg, warning_class)
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-cpython-39
creating build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/extras.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_json.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/__init__.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_range.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_ipaddress.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/sql.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/errors.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/tz.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/pool.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/extensions.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/errorcodes.py -> build/lib.linux-x86_64-cpython-39/psycopg2
running build_ext
building ‘psycopg2._psycopg’ extension
creating build/temp.linux-x86_64-cpython-39
creating build/temp.linux-x86_64-cpython-39/psycopg
gcc -Wno-unused-result -Wsign-compare -DNDEBUG -D_GNU_SOURCE -fPIC -fwrapv -O2 -pthread -Wno-unused-result -Wsign-compare -g -std=c99 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -D_GNU_SOURCE -fPIC -fwrapv -D_GNU_SOURCE -fPIC -fwrapv -O2 -pthread -Wno-unused-result -Wsign-compare -g -std=c99 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fPIC “-DPSYCOPG_VERSION=2.9.5 (dt dec pq3 ext)” -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=80420 -DPSYCOPG_DEBUG=1 -I/home/josbmusc/virtualenv/josbreed_shop/3.9/include -I/opt/alt/python39/include/python3.9 -I. -I/usr/include -I/usr/include/pgsql/server -I/usr/include/libxml2 -c psycopg/adapter_asis.c -o build/temp.linux-x86_64-cpython-39/psycopg/adapter_asis.o -Wdeclaration-after-statement
In file included from psycopg/adapter_asis.c:28:
./psycopg/psycopg.h:31:2: error: #error “Psycopg requires PostgreSQL client library (libpq) >= 9.1”
cc1: warning: unrecognized command line option “-Wno-unused-result”
cc1: warning: unrecognized command line option “-Wno-unused-result”
cc1: warning: unrecognized command line option “-Wno-unused-result”
It appears you are missing some prerequisite to build the package from source.
You may install a binary package by installing ‘psycopg2-binary’ from PyPI.
If you want to install psycopg2 from source, please install the packages
required for the build and try again.
For further information please check the ‘doc/src/install.rst’ file (also at
).
error: command ‘/usr/bin/gcc’ failed with exit code 1
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for psycopg2-binary
Running setup.py clean for psycopg2-binary
Failed to build psycopg2-binary
Installing collected packages: psycopg2-binary
Running setup.py install for psycopg2-binary … error
error: subprocess-exited-with-error
× Running setup.py install for psycopg2-binary did not run successfully.
│ exit code: 1
╰─> [42 lines of output]
/home/josbmusc/virtualenv/josbreed_shop/3.9/lib/python3.9/site-packages/setuptools/config/setupcfg.py:508: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
warnings.warn(msg, warning_class)
running install
/home/josbmusc/virtualenv/josbreed_shop/3.9/lib/python3.9/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build andpip and other standards-based tools.
warnings.warn(
running build
running build_py
creating build
creating build/lib.linux-x86_64-cpython-39
creating build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/extras.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_json.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/__init__.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_range.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_ipaddress.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/sql.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/errors.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/tz.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/pool.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/extensions.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/errorcodes.py -> build/lib.linux-x86_64-cpython-39/psycopg2
running build_ext
building ‘psycopg2._psycopg’ extension
creating build/temp.linux-x86_64-cpython-39
creating build/temp.linux-x86_64-cpython-39/psycopg
gcc -Wno-unused-result -Wsign-compare -DNDEBUG -D_GNU_SOURCE -fPIC -fwrapv -O2 -pthread -Wno-unused-result -Wsign-compare -g -std=c99 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -D_GNU_SOURCE -fPIC -fwrapv -D_GNU_SOURCE -fPIC -fwrapv -O2 -pthread -Wno-unused-result -Wsign-compare -g -std=c99 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fPIC “-DPSYCOPG_VERSION=2.9.5 (dt dec pq3 ext)” -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=80420 -DPSYCOPG_DEBUG=1 -I/home/josbmusc/virtualenv/josbreed_shop/3.9/include -I/opt/alt/python39/include/python3.9 -I. -I/usr/include -I/usr/include/pgsql/server -I/usr/include/libxml2 -c psycopg/adapter_asis.c -o build/temp.linux-x86_64-cpython-39/psycopg/adapter_asis.o -Wdeclaration-after-statement
In file included from psycopg/adapter_asis.c:28:
./psycopg/psycopg.h:31:2: error: #error “Psycopg requires PostgreSQL client library (libpq) >= 9.1”
cc1: warning: unrecognized command line option “-Wno-unused-result”
cc1: warning: unrecognized command line option “-Wno-unused-result”
cc1: warning: unrecognized command line option “-Wno-unused-result”
It appears you are missing some prerequisite to build the package from source.
You may install a binary package by installing ‘psycopg2-binary’ from PyPI.
If you want to install psycopg2 from source, please install the packages
required for the build and try again.
For further information please check the ‘doc/src/install.rst’ file (also at
).
error: command ‘/usr/bin/gcc’ failed with exit code 1
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure
× Encountered error while trying to install package.
╰─> psycopg2-binary
note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.”
i am really stuck, i will appricate if you help me out.