Home > Articles > Open Source > Python

  • Print
  • + Share This
This chapter is from the book

Configuring the Database

After you have verified that you can start and stop the development server, it is time to configure access to the database. This section takes you through the process of creating and configuring access to the database that will be used in the sample project.

Configuring the database involves three major steps. The first is to create the database and assign rights. The second is to modify the settings.py file to specify the database type, name, location, and access credentials. The third step is to synchronize the Django project with the database to create the initial tables necessary for the Django engine.

Django supports several different types of database engines. The project used in this book uses a MySQL database. This section assumes that you have already installed, configured, and started a database engine and that it is accessible from the development server.

Configuring Database Access in settings.py

After the database has been created and a user account set up for Django, you need to configure the settings.py file in your Django project to access that database. Each Django project has its own settings.py file. The settings.py file is a Python script that configures various project settings.

Django uses the following settings in the settings.py file to control access to the database:

  • DATABASE_ENGINE is the type of database engine. Django accepts postgresql_psycopg2, postgresql, mysql, mysql_old, sqlite3, and ado_mssql.
  • DATABASE_NAME is the name of the database. For SQLite, you need to specify the full path.
  • DATABASE_USER is the user account to use when connecting to the database. No user is used with SQLite.
  • DATABASE_PASSWORD is the password for DATABASE_USER. No password is used with SQLite.
  • DATABASE_HOST is the host on which the database is stored. This can be left empty for localhost. No host is specified with SQLite.
  • DATABASE_PORT is the port to use when connecting to the database. This can be left empty for the default port. No port is specified with SQLite.

Synchronizing the Project to the Database

After you have configured access to the database in the settings.py file, you can synchronize your project to the database. Django's synchronization process creates the tables necessary in the database to support your project.

The tables are created based on what applications are specified in the INSTALLED_APPS setting of the settings.py file. The following are the default settings already specified in the INSTALLED_APPS setting:


The following list describes the default applications that get installed in the Django project:

  • django.contrib.auth is the default authentication system included with Django.
  • django.contrib.contenttypes is the framework of types of content.
  • django.contrib.sessions is the framework used to manage sessions.
  • django.contrib.sites is the framework used to manage multiple sites using a single Django installation.
  • + Share This
  • 🔖 Save To Your Account