Using Databases with Django

PostgreSQL

To use a PostgreSQL database with your Django project:

1

Create a new PostgreSQL database and make a note of the following:

  • The database name
  • The database user name
  • The database user password
2

SSH into your Django application's shell user account and run the following commands to install the PostgreSQL dependencies for Python (substituting myapp with your Django application name):

source ~/apps/myapp/env/bin/activate
pip install psycopg2-binary
3

Configure DATABASES in your project's settings.py as follows:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'your_database_name',
        'USER': 'your_database_user_name',
        'PASSWORD': 'your_database_user_password',
        'HOST': '',
        'PORT': '',
    }
}

MariaDB (MySQL)

Opalstack uses MariaDB for MySQL-compatible database services. To use a MariaDB database with your Django project:

1

Create a new MariaDB database and make a note of the following:

  • The database name
  • The database user name
  • The database user password
2

SSH into your Django application's shell user account and run the following commands to install the MariaDB dependencies for Python (substituting myapp with your Django application name):

source ~/apps/myapp/env/bin/activate
pip install mysqlclient
3

Configure DATABASES in your project's settings.py as follows:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'your_database_name',
        'USER': 'your_database_user_name',
        'PASSWORD': 'your_database_user_password',
        'HOST': '',
        'PORT': '',
    }
}

SQLite

The CentOS operating system used by Opalstack includes a version of SQLite that is too old to be compatible with most current web applications.

To work around this, you can install an updated SQLite binary package for Python and configure Django to use it as shown below.

To use a SQLite database with your Django project:

1

SSH into your Django application's shell user account and run the following commands to install the PostgreSQL dependencies for Python (substituting myapp with your Django application name):

source ~/apps/myapp/env/bin/activate
pip install pysqlite3-binary
2

Configure DATABASES in your project's settings.py as follows:

# include the next 3 lines in your settings.py just before the DATABASES dict
__import__('pysqlite3')
import sys
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')

# then configure the engine and path to the DB here
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}