In this article:
- Create AWS RDS Postgres Instance
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.
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.
In "Settings" section:
- pick a name for your database instance. It should be unique for your AWS account. Let's use
- pick master username and password that will be hard to guess
In DB instance sections we don't have a choice for free tier, so we have db.t2.micro option available only.
In Storage section you can change the allocated storage if you plan to have a lot of data.
Pay attention to Connectivity section.
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
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.
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.
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
The value of
DATABASE_URL is build on this pattern:
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.
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.
Check if connection to database was successful
Click "Manage Deployments" to see deployment logs and see if our database change was a success.
Find the latest deployment, first in the list and click "View 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" (22.214.171.124) and accepting TCP/IP connections on port 5432?
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.
On the server detail page, select and copy server IP address that is located under the server name.
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.
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.
You will be taken to Security Group Detail page.
On the tab "Inbound Rules" click the button "Edit inbound rules"
You will be taken to the page "Edit inbound rules".
Click the "Add rule" button.
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.
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!