In this article:
Introduction Database is the main auxiliary service needed for running your Django Project.
Appliku allows you to spin up a database on your own servers and it comes at no cost (except server resources).
If your app requires a production-grade database, then AWS RDS is a popular choice.
In this article we will walk through a Postgres RDS instance creation and adding DB credentials to your Django app.
Requirements¶
- You should have a Django app deployed on Appliku
- Repository we are using for this tutorial: https://github.com/appliku/django-docker-tutorial
Also read:
Create AWS RDS Postgres Instance¶
Go to AWS RDS Dashboard, then click Databases in the left sidebar.
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".
- Version: Pick the latest available version, at the moment of writing this article it is 17.2
- In "Templates" section let's pick "Free tier" to avoid any costs while going over this tutorial.
In the settings section:
- pick a name for your DB instances,
- pick a name for the superuser, credentials management: self-managed
- set the master password
DB Instance class: We don't really have any options for the free tier:
Storage class section: Here specify how much storage your DB needs, minimum is 20GB
Pay attention to the Connectivity section.
Turn on Public Access.
Create a new VPC Security Group and give it a name
You may use your security group settings later to limit IPs that can connect to your RDS Instance.
In the Database authentication leave the 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.
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 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.
Edit Security Group to allow connection to DB from our server¶
On the detail page of our AWS RDS Instanace find the section "Security Group Rules", find type: "CIDR/IP - Inbound" Inbound row and click on the security group name.
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.
You can find your IP address in Appliku Dashboard on the server page or on the Application overview page in the "Server" block on the right
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.
Set DATABASE_URL for RDS Postgres Instance¶
Now in Appliku Dashboard, go to Settings of your application, find section "Environment Variables".
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.
Deploy Django application with AWS RDS Postgres Instance¶
Then click Save and deploy.
The deployment process will start
Go to the deployment logs to see that migrations got applied, this means that our new database works correctly.
Also read: