In this article:

Introduction

Database is the main auxiliary service needed for running your Django Project.

Appliku provides a you with feature to spin up a postgres database that comes at no cost.

But that is mostly suitable for staging/non-production use cases or pet projects.

If a certain app requires a production-grade database, then the best choice is to use AWS RDS.

In this article we will make a Postgres RDS instance make our Django Project use this database.

Create AWS RDS Postgres Instance

Go to AWS RDS Dashboard https://eu-central-1.console.aws.amazon.com/rds/home?region=eu-central-1#databases:

Make sure you have correct region selected in the right part of top navigation. In this example it is set to Frankfurt (eu-central-1).

Click the "Create database" button.

Create AWS RDS Postgres

On the creation form:

  • For "Choose a database creation method" choose "Standard Create"
  • For "Engine Options" choose "PostgreSQL".
  • In "Templates" section let's pick "Free tier" to avoid any costs while going over this tutorial.

Free tier RDS Postgres Engine

In "Settings" section: - pick a name for your database instance. It should be unique for your AWS account. Let's use djangoapplikututorial - pick master username and password that will be hard to guess Create AWS RDS Postgres instance Settings

In DB instance sections we don't have a choice for free tier, so we have db.t2.micro option available only. AWS RDS Postgres DB instance class db.t2.micro

In Storage section you can change the allocated storage if you plan to have a lot of data.

AWS RDS Postgres Storage Allocated Storage

Pay attention to Connectivity section.

AWS RDS Postgres Connectivity Public Access

Enable Public access by selecting "Yes".

Create new VPC security group.

New VPC secutiry group name you can set to "djangotutorialsg".

We might need security group to limit access to our database server.

In section "Database authentication" leave option "Password Authentication" selected

AWS RDS Postgres Database authentication Password Authentication

In section Additional configuration:

Set field "Initial Database name" to the name of your liking. For purpose of this article we'll call it "djangoapplikututorial"

Make sure that "Enable automatic backups" is enabled.

Choose backup retention period. Every additional day will increase amount of storage used, thus might increase storage costs.

You can leave the rest of options as they were. RDS Postgres Additional configuration and backups

RDS Postgres Additional configuration Performance Insights, Monitoring and Logs

Estimated monthly costs section should say how much it costs. SInce we picked the free tier, this section will not show any monthly cost.

Click Create Database button. You will get to the list of your databases. Please wait until Status field will say "Available".

Find out connection details

Now click on the database to see the details about DB.

AWS RDS Postgres Instance information endpoint and port

Pay attention to Endpoint and Port section. Endpoint will be your DB HOST and Port will be Port in the credentials URL.

In order for your Django App to connect to this database you need to set environment variable DATABASE_URL

The value of DATABASE_URL is build on this pattern: postgresql://USERNAME:PASSWORD@DB_HOST:DB_PORT/DATABASE_NAME

So USERNAME is the value you have given the field Master Username, PASSWORD - Master Password.

DATABASE_NAME comes from the "Initial Database name" field, you filled during RDS Postgres Instance creation.

DB_HOST comes from the value of Endpoint on database details page.

DB_PORT comes from Port value on database details page.

Set DATABASE_URL for RDS Postgres Instance

Now in your Appliku Deploy account, go to Settings of your application, find section "Config Variables" and click "REVEAL CONFIG VARS".

Add or update variable "DATABASE_URL" to the value of your credential string. Paste carefully, make sure you don't have spaces anywhere in the string.

Click "Update" button for this variable row.

AWS RDS Postgres URL environment variables for Django Project

The variable is saved and now we need to apply our environment variable changes.

Go to application overview.

If you haven't deployed the app yet, click "Deploy Now"

If you already have deployed the app and just need your settings get applied, faster way is to click "Apply Processes & Env Vars" button.

Apply changes to Environment Variables for Django Project

Check if connection to database was successful

Click "Manage Deployments" to see deployment logs and see if our database change was a success.

Manage Django Deployments

Find the latest deployment, first in the list and click "View Logs".

View Django Project deployment logs

If you scroll down you will see that app had trouble connecting to database:

There was an error: could not connect to server: Connection timed out
    Is the server running on host "djangoapplikututorial.cgppyigsg4ks.eu-central-1.rds.amazonaws.com" (18.198.177.60) and accepting
    TCP/IP connections on port 5432?

There was an error: could not connect to server: Connection timed out

This happens because the security group for our database doesn't allow connection to the database from our server. Let's add application's server IP address to the list of allowed to connect.

Get your server IP address

First, let's find out the IP address for our server. In order to do this, go to application overview and find the server and click on it.

Django Application Overview open server details

On the server detail page, select and copy server IP address that is located under the server name.

Django Server IP address

Add application server IP address to Security Group Inbound rules

Go back to the AWS RDS interface to our Instance detail page.

In the section "Security Group Rules" find Type: CIDR/IP - Inbound row and click on the Security Group name.

AWS RDS CIDR/IP - Inbound Security Group

You will be taken to Security Groups list. It is filtered to show only the one we are interested in.

Click on the Security Group ID.

AWS EC2 Security Groups

You will be taken to Security Group Detail page.

On the tab "Inbound Rules" click the button "Edit inbound rules"

AWS RDS Security Group Edit Inbound Rules

You will be taken to the page "Edit inbound rules".

Click the "Add rule" button.

Add inbound rules

In the new row, open "Type" dropdown and found "PostgreSQL". Source: "Custom" Paste your server IP address in the field after "Source" and select the suggestion from dropdown with server IP + /32.

Click Save rules.

Add inbound rule to security group

Deploy Django Application with new AWS RDS Postgres instances

Go back to Appliku Deploy interface and click "Apply Processes & Env Vars". Then click on "Manage deployments" and open latest deployment's log.

This time the end of the log will be lines with applying migrations, which means that our Django App has successfully connected to database!

Django Apply Migrations to AWS RDS Postgres instance