Chapter 6. Deploy Django App

Production Database Setup

Production Database Setup
Tag:

To change a database from the default database SQLite, you need to install database software that you want to use in the production stage (e.g., PostgreSQL). You also need to do the initial setup for the database.

Note: Most actions from this lesson onwards must be done on the Ubuntu instance. Make sure that you are connected to the remote server via SSH.

1. Install PostgreSQL on the instance

Based on the PosgtreSQL official documentation, you can install PostgreSQL in the following steps.

1. Create the file repository configuration

Command Line - INPUT
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

2. Import the repository signing key

Command Line - INPUT
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

3. Update the package lists

Command Line - INPUT
sudo apt-get update

4. Install the latest version of PostgreSQL

Command Line - INPUT
sudo apt-get -y install postgresql

You can check if the PosgreSQL is properly installed and running. First, check the version installed by the psql --version command.

Command Line - INPUT
psql --version
Command Line - RESPONSE
psql (PostgreSQL) 15.2 (Ubuntu 15.2-1.pgdg20.04+1)

Using the systemctl command, check if PostgreSQL is running as a demon process.

Command Line - INPUT
systemctl status postgresql
Command Line - RESPONSE
 postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2023-04-20 13:13:41 UTC; 2min 20s ago
Main PID: 8430 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 2372)
Memory: 0B
CGroup: /system.slice/postgresql.service

Apr 20 13:13:41 ip-172-26-0-231 systemd[1]: Starting PostgreSQL RDBMS...
Apr 20 13:13:41 ip-172-26-0-231 systemd[1]: Finished PostgreSQL RDBMS.

Check the official document for more details about the installation process.

PostgreSQL documentation reference: Linux downloads (Ubuntu)

2. Create a database and user for the project

You need to create a database and user for the project. As the parameters will be used in the Django settings later, copy and save them somewhere.

Start PostgreSQL command prompt

First, you need to go into PostgreSQL using the following command. The prompt changes to postgres=#. You can type the PostgreSQL commands in this mode.

Command Line - INPUT
sudo -u postgres psql
Command Line - INTERACTIVE
psql (15.2 (Ubuntu 15.2-1.pgdg20.04+1))
Type "help" for help.

postgres=#

Create a database for the project

To create a database for the project, run the command below. Don't forget ; at the end.

Command Line - INPUT
CREATE DATABASE project_d;

When this is completed successfully, the command line returns the message below.

Command Line - RESPONSE
CREATE DATABASE

Create a user for the project

To create a user for the project, run the command below with the password you want to set.

Command Line - INPUT
CREATE USER project_d_user WITH PASSWORD 'project_d_pass';

When this is completed successfully, the command line returns the message below.

Command Line - RESPONSE
CREATE ROLE

Make sure that you keep the username and password for the settings later.

Add user privileges for the database

You also need to grant all privileges for the database to the newly created user.

Command Line - INPUT
GRANT ALL PRIVILEGES ON DATABASE project_d TO project_d_user;

When this is completed successfully, the command line returns the message below.

Command Line - RESPONSE
GRANT

Alter the database owner

This setting was not required in the older version of PostgreSQL. Now, you need to add this setting by running the command below.

Command Line - INPUT
ALTER DATABASE project_d OWNER TO project_d_user;

If you don't set this, you may encounter an error in the database migration process. When this is completed successfully, the command line returns the message below.

Command Line - RESPONSE
ALTER DATABASE

Exit the prompt

Type \q to exit from the PostgreSQL prompt.

Command Line - INPUT
\q

3. Update the Django database setting

You also need to update the database settings in Django. You need to make sure that the parameters set through the PostgreSQL prompt are the same (i.e., NAME, USER, PASSWORD). Below is an example of the database settings. You don't need to update the settings.py file yet, as we'll explain how to create two settings files for development use and production use in the following pages.

The yellow part is a part different from the original settings.py, and the blue part is the one you'll need to set for the PostgreSQL. We'll explain how to execute this on the following pages.

config/settings.py
    :

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'project_d',
        'USER': 'project_d_user',
        'PASSWORD': 'project_d_pass',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

To change a database from the default database SQLite, you need to install database software that you want to use in the production stage (e.g., PostgreSQL). You also need to do the initial setup for the database.

Note: Most actions from this lesson onwards must be done on the Ubuntu instance. Make sure that you are connected to the remote server via SSH.

1. Install PostgreSQL on the instance

Based on the PosgtreSQL official documentation, you can install PostgreSQL in the following steps.

1. Create the file repository configuration

Command Line - INPUT
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

2. Import the repository signing key

Command Line - INPUT
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

3. Update the package lists

Command Line - INPUT
sudo apt-get update

4. Install the latest version of PostgreSQL

Command Line - INPUT
sudo apt-get -y install postgresql

You can check if the PosgreSQL is properly installed and running. First, check the version installed by the psql --version command.

Command Line - INPUT
psql --version
Command Line - RESPONSE
psql (PostgreSQL) 15.2 (Ubuntu 15.2-1.pgdg20.04+1)

Using the systemctl command, check if PostgreSQL is running as a demon process.

Command Line - INPUT
systemctl status postgresql
Command Line - RESPONSE
 postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2023-04-20 13:13:41 UTC; 2min 20s ago
Main PID: 8430 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 2372)
Memory: 0B
CGroup: /system.slice/postgresql.service

Apr 20 13:13:41 ip-172-26-0-231 systemd[1]: Starting PostgreSQL RDBMS...
Apr 20 13:13:41 ip-172-26-0-231 systemd[1]: Finished PostgreSQL RDBMS.

Check the official document for more details about the installation process.

PostgreSQL documentation reference: Linux downloads (Ubuntu)

2. Create a database and user for the project

You need to create a database and user for the project. As the parameters will be used in the Django settings later, copy and save them somewhere.

Start PostgreSQL command prompt

First, you need to go into PostgreSQL using the following command. The prompt changes to postgres=#. You can type the PostgreSQL commands in this mode.

Command Line - INPUT
sudo -u postgres psql
Command Line - INTERACTIVE
psql (15.2 (Ubuntu 15.2-1.pgdg20.04+1))
Type "help" for help.

postgres=#

Create a database for the project

To create a database for the project, run the command below. Don't forget ; at the end.

Command Line - INPUT
CREATE DATABASE project_d;

When this is completed successfully, the command line returns the message below.

Command Line - RESPONSE
CREATE DATABASE

Create a user for the project

To create a user for the project, run the command below with the password you want to set.

Command Line - INPUT
CREATE USER project_d_user WITH PASSWORD 'project_d_pass';

When this is completed successfully, the command line returns the message below.

Command Line - RESPONSE
CREATE ROLE

Make sure that you keep the username and password for the settings later.

Add user privileges for the database

You also need to grant all privileges for the database to the newly created user.

Command Line - INPUT
GRANT ALL PRIVILEGES ON DATABASE project_d TO project_d_user;

When this is completed successfully, the command line returns the message below.

Command Line - RESPONSE
GRANT

Alter the database owner

This setting was not required in the older version of PostgreSQL. Now, you need to add this setting by running the command below.

Command Line - INPUT
ALTER DATABASE project_d OWNER TO project_d_user;

If you don't set this, you may encounter an error in the database migration process. When this is completed successfully, the command line returns the message below.

Command Line - RESPONSE
ALTER DATABASE

Exit the prompt

Type \q to exit from the PostgreSQL prompt.

Command Line - INPUT
\q

3. Update the Django database setting

You also need to update the database settings in Django. You need to make sure that the parameters set through the PostgreSQL prompt are the same (i.e., NAME, USER, PASSWORD). Below is an example of the database settings. You don't need to update the settings.py file yet, as we'll explain how to create two settings files for development use and production use in the following pages.

The yellow part is a part different from the original settings.py, and the blue part is the one you'll need to set for the PostgreSQL. We'll explain how to execute this on the following pages.

config/settings.py
    :

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'project_d',
        'USER': 'project_d_user',
        'PASSWORD': 'project_d_pass',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
Tag: