Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Django IntroductionChapter 6. Deploy Django App

Production Database Setup

Production Database Setup

Setting Up a Production Database for Django

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',
    }
}

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Django Templates vs. Django APIs for Web Development

Django Templates vs. Django APIs

Key Steps for Deploying a Django App

Key Steps of Django App Deployment

Managing Static Files in Django with {% static %}

Static Files in Development Environment – {% static %} tag

Understanding the Difference Between Django Projects and Apps

Project vs. App

Django Templates vs. Django APIs for Web Development

Django Templates vs. Django APIs

Key Steps for Deploying a Django App

Key Steps of Django App Deployment

Managing Static Files in Django with {% static %}

Static Files in Development Environment – {% static %} tag

Understanding the Difference Between Django Projects and Apps

Project vs. App

Tags:

Daemon Process

settings.py

Production Environment

PostgreSQL

App Deployment

Development Environment

SQLite

Django Introduction
Course Content

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment