Learn Django

Configure the Database

With Postgres installed you can now configure the database. This includes creating the database to be used in this course, telling Django the details about this database, and executing a Django command that sets up several required tables in the database.

Step 1: Use pgAdmin to Create a Database

In the previous lesson you installed the Postgresql database engine. One of the tools installed with this is pgAdmin. This tool allows you to manage the database. One of the things you can do with this tool is create a database. That's what you'll do in this step.

  1. On your Mac or Windows, search for the pgAdmin application. This was installed with PostgreSQL
  2. When you open this app, expand the nodes in the 'Object browser' until you see the localhost connection. There might have a red x on it. Double click it
  3. Enter the password when prompted (you chose this password during installation)

  1. Right click on Databases, and select New Database...

  1. In the New Database... dialog box, give it a name of crmeasyDB. Leave the other settings at their defaults

Step 2: Configure Settings.py Database Settings

In this step you'll configure the database in Django's settings file. Django needs to know a few things about the database, like what engine is being used, the database name, and password used to access it. Follow these steps to configure those settings.

  1. Open the settings.py file in your IDE
  2. Locate the DATABASES setting and configure it as you see below.

NOTE: Update the PASSWORD value to the one you setup during Postgresql installation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'crmeasyDB',
        'USER': 'postgres',
        'PASSWORD': 'warning this is where you need to put your password',
        'HOST': '/tmp',
        'PORT': '5432',
    }
}

Step 3: Commit the Initial Migration

Django 1.7 has a brand new system for managing the database. This system makes it much easier to make changes to database schemas than previous versions of Django. Usually, the migration process is two steps. In later chapters you'll learn how to perform both migration steps. However, during the initial project setup, you only have to run one of the steps.

Execute these commands at your terminal or command prompt. Make sure the virtual environment is activated before issuing these commands.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# make sure you're in the crmeasy folder
(venv)$ pwd
/.../crmeasy

# commit the initial migration 
(venv)$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying sessions.0001_initial... OK

Step 4: Create the Superuser

Django projects need a superuser. This user will have control over every aspect of Django, so it's important to set this up. While you can have an easy password in your development environment, it's important to have a strong password on production systems.

1
2
3
4
5
6
7
# create the superuser
(venv)$ python manage.py createsuperuser
Username: example
Email address: example@example.com
Password: 
Password (again):
Superuser created successfully.

Step 4: Commit Changes

Execute these commands to commit your changes in Git.

1
2
3
4
5
# add files
(venv)$ git add .

# commit files
(venv)$ git commit -m "configured DB settings"

Track your progress with a free account