Chris Shaw

Chris Shaw

Lead Profile All Articles

I am an ardent software developer and eager lifelong learner of code. My passion is the python language with particular emphasis in the 'django' and  'opencv' packages. I have created this blog to share my e... Read More


Connecting a django project to a shared server database

Feb. 12, 2019 Chris Shaw 1836 views

In a previous article, I showed how to easily deploy django on a shared server. Shared servers also provide a options of database servers that you can use for your project.

Install the required python packages for selected database engine

The first line is for MySQL and the second for PosgreSQL. You only need to select one but there is no harm in installing both. I have specified the version of mysqlclient as I have found the latest to give errors during installation.

From the command line, in our virtual enviroment:

# Mysql for django versions upto 2.1 
(domain_html:3.5)username@server[~/domain_html]# pip install mysqlclient==1.3.12

# Mysql for django versions 2.2 or higher
(domain_html:3.5)username@server[~/domain_html]# pip install mysqlclient

(domain_html:3.5)username@server[~/domain_html]# pip install psycopg2

Create the database and database user

From your cPanel, type 'databases' in the search field and click on your preferred option.

First, create a new database for your project. I recommend using a name similar to that of your domain. Click 'Create Database' to create the database.

Next, create a username for the database. Use the 'Password Generator' to create secure random passwords. Make sure you sone the username and password somewhere safe. Click 'Create User' to create the user.

Lastly, we will add the user to the database and set the privileges. Once you have selected the User and Database and clicked Add, you will be asked what privileges to assign the user for the database. For now, select 'ALL PRIVILEGES' and click 'Make Changes'. You can always scale back the privileges later.

We now have our database and user. The images above are from the MySQL option and the PostgreSQL option is very similar. Now we can edit our django project settings to use this database.


Edit the project file:

(domain_html:3.5)username@server[~/domain_html]# vim mysite/ 

Find the default database settings:

    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

And for MySQL, replace with:

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'username_yourdbname',
        'USER': 'username_yourdbusername',
        'PASSWORD': 'youruserpassword',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'sql_mode': 'STRICT_ALL_TABLES',

Or for PostgreSQL, replace with:

    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'username_yourdbname',
        'USER': 'username_yourdbusername',
        'PASSWORD': 'youruserpassword',
        'HOST': 'localhost',
        'PORT': '5432',


Migrating will create the tables on our connected database server, and in effect test our settings. First we make migrations and then migrate. If there are no errors, our database is working correctly.

(domain_html:3.5)username@server [~/domain_html]# python makemigrations
No changes detected
(domain_html:3.5)username@server [~/domain_html]# python migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK


And that is it. The django project on our shared sever is using the database server. QED.

Related Articles

blog comments powered by Disqus