Setting Up a Relational Database with RDS

As your application moves into production, relying on a Dockerized PostgreSQL instance inside your EC2 server introduces limitations—especially in scalability, backups, and long-term maintenance. Amazon RDS (Relational Database Service) provides a managed solution that simplifies database operations while integrating smoothly with your custom VPC setup.
In this section, you'll create a PostgreSQL database instance inside of your custom VPC and connect it to your Django image-sharing app.
Choosing the right engine in RDS
When creating a database in Amazon RDS, you start by choosing a database engine. You'll see two PostgreSQL-related options:
PostgreSQL: This is the standard open-source engine you're already using in your Docker container. Amazon RDS handles the installation, patching, backups, and scaling for you—while keeping the PostgreSQL experience familiar.
Aurora PostgreSQL-Compatible: This is a cloud-optimized version of PostgreSQL developed by AWS. It's faster and offers automatic scaling, but is more complex and expensive. It's a great option later when your app grows and requires high performance.
What we’re using
For this guide, we’ll use Amazon RDS with PostgreSQL, as it’s simpler, more cost-effective, and ideal for applications just moving into production. You can upgrade to Aurora later if needed.
Setting up and connecting your RDS database
In this section, you’ll configure and connect your Amazon RDS database to your Django application. We’ll walk through provisioning the database, adjusting network permissions, updating your app settings, and confirming everything works correctly—all using beginner-friendly defaults. By the end, your app will be backed by a managed cloud database instead of a local container.
Step1: Creating the RDS database and adjust security group settings in AWS
Before your Django app can connect to a cloud-based database, you need to provision one using Amazon RDS. This section walks you through setting up a PostgreSQL instance inside your custom VPC, using Free Tier–eligible settings and configuring security group rules to allow safe access from your EC2-based application server.
Creating the RDS database
In this part, we'll walk through creating a PostgreSQL database using Amazon RDS. You’ll configure database settings, select the right VPC and subnets, and prepare everything needed to support your Django app securely and reliably.
In the top search bar, type RDS and select Aurora and RDS.

You’ll land on the Aurora and RDS Dashboard. Click Create database.

Under Database creation method, choose Standard Create. In the Engine options, select PostgreSQL.

Choose the latest version that matches your development version (e.g., PostgreSQL 15.x).
Note: This matches the PostgreSQL 15 image used in your Docker container and helps prevent compatibility issues.

For Templates, select Free tier for the practice purposes. This template automatically configures a single Availability Zone and other settings to stay within Free Tier limits.

For Settings, set the following parameters.
- DB instance identifier:
image-sharing-db
- Master username:
postgres
- Credential management: Choose Self managed to avoid Secrets Manager charges.
- Master password: Enter a secure password. You'll use this in your
.env.prod
file later.

For Connectivity,
- Compute resource setup: Select Don’t connect to an EC2 compute resource (you’ll connect manually through your Django app).
- Network type: Keep as IPv4 (default).
- VPC: Select your custom VPC (e.g.,
image-sharing-app-vpc
). - DB subnet group: Select Create new DB Subnet Group.
- Public access: Select No (keep the database private).
- VPC security group: Select Choose existing, and then pick the same security group used by your EC2 instance. (We’ll adjust the inbound rules for this group right after the database is created.)

Leave all other settings as they are. Scroll down to review the Estimated monthly costs to ensure Free Tier eligibility, then click Create database.
Note: We're intentionally keeping most settings at their default values to simplify the learning process and avoid overwhelming you with too many options at once. While advanced configurations like backup retention, enhanced monitoring, and parameter tuning are valuable in production, they’re not essential for this beginner-friendly setup.

After a few minutes, your application's database should be ready.

Add inbound rules to security group
Now that the database has been created, you'll need to configure the security group so your EC2 instance can connect to RDS.
In the left sidebar, click Security Groups. Find and select the security group used for the EC2 instance in the custom VPC.

Go to the Inbound rules tab and click Edit inbound rules.

- Type: PostgreSQL
- Source: Choose Custom and then select the same security group name (not a CIDR or IP).

Click Save rules. Changes to security groups apply immediately.
Step2: Creating a backup of your data
Before migrating to an RDS database, it's important to back up your existing application data—especially if you've already been using the app. One easy way to do this is by using the data export feature in the Django admin.
Navigate to the Data Management section, then go to the Export page and select Export all data. Depending on your browser settings, the download may be blocked or delayed. Be sure to check your browser for any messages or prompts that ask for download confirmation.

Step 3: Update database settings in the app
Now that the database is up and secure, it’s time to point your Django app to the new RDS instance.
Update .env.prod for RDS
Now that your RDS database is running, update your Django app’s environment file to connect it to the cloud-hosted PostgreSQL instance.
Open your .env.prod
file and replace the local Docker-based PostgreSQL values with the following:
DB_NAME=postgres
DB_USER=postgres
DB_PASSWORD=your_secure_password
DB_HOST=image-sharing-db.xxxxxxxxxxxx.us-east-1.rds.amazonaws.com
DB_PORT=5432
DB_NAME: For simplicity, we’re using the default postgres
database that RDS creates automatically.
Note: In the actual project, it’s best practice to use a separate database name like imageappdb
or image-sharing-db
for better organization and access control. You can do this by specifying the "Initial database name" during RDS setup.
DB_USER: Set this to the master username you provided during setup, which is postgres
.
DB_PASSWORD: Enter the master password you created when setting up the database.
DB_HOST: This is your RDS endpoint (the hostname that your app will connect to).
- Go to the RDS Console
- Click on your database instance (e.g.,
image-sharing-db
) - In the Connectivity & security section, copy the value listed under Endpoint
DB_PORT: Leave as 5432
, the default port for PostgreSQL.

This configuration allows your Django app to connect directly to the RDS database instead of the local PostgreSQL container. Let me know if you'd like a clarification added for cases where a custom DB name was created during setup.
Disable docker’s PostgreSQL service
In your docker-compose.prod.yml
, remove or comment out the following:
- In the
web
service, thedepends_on: - db
line. - the
db
service section.

Rebuild the containers without the PostgreSQL container:
docker-compose -f docker-compose.prod.yml --env-file .env.prod down
docker-compose -f docker-compose.prod.yml --env-file .env.prod up --build -d
Apply migrations to RDS
Once the app is running, apply your Django database migrations:
docker-compose -f docker-compose.prod.yml --env-file .env.prod exec web python manage.py migrate
Then create a superuser.
docker-compose -f docker-compose.prod.yml --env-file .env.prod exec web python manage.py createsuperuser
Step 4: Confirm everything works
Visit your deployed site using the public IP of your EC2 server.
Open your browser and go to: http://<your-ec2-ip>
As the original data is not carried, you should see the image sharing app’s homepage without any uploads.

To log into the admin panel: http://<your-ec2-ip>/admin
.

Import backup data
If you exported your data before migrating to RDS, you can import it back using the Django Admin. Alternatively, you can use our provided test dataset—Download the data via this link.
When importing, make sure to import user data first, since image data is linked to user accounts. If the image files are already stored in Amazon S3, there's no need to reupload them. Just ensure that the imported data correctly references the existing file paths in S3.

Once the import is complete, you can confirm that your Django app is now fully connected to Amazon RDS for the database and Amazon S3 for media storage.
Summary
You've just taken a major step toward production-readiness. After migrating from a local Dockerized PostgreSQL database to Amazon RDS, your Django image-sharing app is now backed by a fully managed, scalable, and secure relational database.
In the next section, we’ll look at distributing application traffic using Elastic Load Balancing to help your app handle more users reliably.
Keep moving forward—your infrastructure is becoming cloud-native, one service at a time.