How to connect Django application to PostgreSQL Databases

Procedure

1. Access your Cpanel and create a PostgreSQL Database and add a database user. See the guides on: https://truehost.com/support/knowledge-base/how-to-create-a-postgresql-database-in-the-cpanel/ and https://truehost.com/support/knowledge-base/how-to-create-a-postgresql-user-adding-them-to-the-database/

2.Install the required dependency/add <psycopg2-binary> to your requirements.txt

pip install psycopg2-binary

3. Go to your settings.py :: This is found in the folder that was generated on running > django-admin startproject appname:: inside the appname/root folder for your project, there is another folder named appname, where the settings.py is.

4. Replace the default database configuration with the following:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'DBNAME',
'USER': 'DBUSER',
'PASSWORD': 'DBPASS',
'HOST': 'localhost',
'PORT': '',
}
}

Replace the DBNAME, DBUSER and DBPASS with the details set up in step 1.

Note that other Django commands do not change. Proceed to do the migration and other necessary setups. See how to deploy Django application on shared hosting here: https://truehost.com/support/knowledge-base/how-to-deploy-django-web-application-on-shared-hosting-cpanel/

Was this article helpful?

Related Articles

Leave A Comment?