Skip to content

CMS Database connection

Grace-Amondi edited this page Apr 28, 2021 · 5 revisions

Mukau CMS connects to it's database using the following environment variables which correspond to the respective keys within Django’s DATABASES settings dictionary:

DATABASE_ENGINE
DATABASE_NAME
DATABASE_USER
DATABASE_PASSWORD
DATABASE_HOST
DATABASE_PORT 

The settings directory can be accessed at: https://github.com/icpac-igad/mukau-cms/blob/main/mukau_wagtail_cms/settings/base.py

Below is an example:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

The database can also be accessed using Psycopg. It is a PostgreSQL database adapter for the Python programming language.

Below is an example of a python script to connect to the cms database with psycopg2:

import psycopg2

# Try to connect
conn = None
try:
    conn=psycopg2.connect(dbname='mydatabase', user='mydatabaseuser', port=5432, password='mypassword')

    # Open a cursor to perform database operations
    cur = conn.cursor()
    # execute sql
    cur.execute("""SELECT * from bar""")
    cur.close()
except (Exception, psycopg2.DatabaseError) as error:
    print(error)
finally:
    if conn is not None:
        conn.close()
        print('Database connection closed.')

Clone this wiki locally