Connecting the database

Every environment will have their own database. On Heroku, the database will be created automatically. Locally we have to create the database on our own.

Creating the local PostgreSQL database

As described in [Ignore the SQLite database], we ignored the SQLite database to create our own PostgreSQL database. So let’s do this.

Open postgres shell:

Terminal
psql
If the psql command fails, you may have forgotten to start the Postgres server.

You can use your own database name. I like to use a database name that is a combination of the project name and the environment. That way it is easy to find it when there are multiple databases. And it is clear which environment the database is meant for.

Create the database:

Terminal
CREATE DATABASE djangoku_local;
Do not forget the ; at the end

Quit postgres shell:

Terminal
\q

Required database modules

For Django and Heroku to be able work with PostgreSQL databases, we need two additional modules.

psycopg2 is a database adapter, that enables Django to talk to the PostgreSQL database. dj-database-url allows us to use database urls. This is not important in our local environment, but will be on Heroku. Heroku stores the database url in an environment URL, that we access in the Django settings.

Luckily we added both modules in the chapter [Handling static files] already. You can check requirements.txt if they are present and revisit [Add django-heroku] if not.

Connecting Django and the database

Now that we have the prerequisites for our PostgreSQL database in place, it is time to clean up a bit.

Deleting the old …

Finally we can get rid of the db.sqlite3 database. Just make sure that you first follow the steps to add the PostgreSQL databas below, before restarting the Django server. If you don’t Django will just recreate the file.

Terminal
rm db.sqlite3

… adding the new

The data Django needs to connect to the PostgreSQL database is sensitive information. That’s one reason why we will store them in our .env file. Another advantage of this is that we can connect to different databases on different environments.

We will add a DATABASE_URL variable to the .env file, that contains all information to connect to the database. It will look like this: DATABASE_URL=postgres://{user}:{password}@{host_name}:{port}/{database_name

Before we add it to the .env file, let’s look at the segments of the DATABSE_URL:

Segment

Info

Default value

user

The user you want to connect with to the database

postgres

password

The password for the user

` ` (no password)

host_name

The database host

localhost

port

The database port

5432

database_name

The database name you chose before

djangoku_local

Let’s combine the segments to a functional url and add it to .env:

.env
export DATABASE_URL=postgres://postgres@localhost:5432/djangoku_local

Now we need to tell Django to use this database instead of the standard SQLite database by importing dj_database_url into djangoku/settings.py and adjusting the DATABASES variable.

Import dj_database_url:

djangoku/settings.py (At the beginning of the file)
import dj_database_url

Replace the current value of DATABASES with this:

djangoku/settings.py (Around line 78)
DATABASES = {
    'default': dj_database_url.config(conn_max_age=600)
}

Try python manage.py runserver to see if Django starts as expected.

If you deleted db.sqlite3 before starting the server, it should not be recreated by Django automatically on launch. If it does, you need to check your settings again.

Commit updates

If you run git status you will see, that we changed requirements.txt and djangoku/settings.py.

Now we can stage and commit all changes:

Terminal
git add .
git commit -m "Connect PostgreSQL database 🗄"

Checklist

✔︎ db.sqlite3 is deleted

Terminal
test -f db.sqlite3 || echo "deleted"

→ Outputs "deleted"

✔︎ PostgreSQL database is present

Terminal
psql --list

djangoku_local is present in the database list output.

✔︎ DATABASE_URL is connected

Terminal
python manage.py check --database

✔︎ Django runs correctly

Terminal
python manage.py runserver

→ Development server starts

results matching ""

    No results matching ""