RDS: A Managed Database in the Cloud
Create a private PostgreSQL database on RDS, connect from EC2, handle credentials safely, and understand backups, Multi-AZ, replicas and costs.
Video chapters
- 00:00Intro
- 00:41Database on EC2, or RDS?
- 01:41RDS in six words
- 02:38Step 1: Create the database
- 03:53How the database stays private
- 04:44Step 2: Connect from EC2
- 05:33Step 3: Credentials, the right way
- 06:35Backups and high availability
- 07:43Which AWS database, when?
- 08:59What RDS costs, and cleaning up
- 09:53Compute, storage, and now a database
Servers and storage are two of the three core building blocks. The third is a database. In this episode you create a managed PostgreSQL database with RDS, keep it private, connect to it from an EC2 server, and learn how AWS handles backups and failover for you.
What you need
- AWS account + admin user (Episode 3)
- The EC2 server from Episode 4 (or launch a new one)
Database on EC2, or RDS?
You could install PostgreSQL on an EC2 server yourself. So why use RDS?
Patching. RDS applies database engine updates for you, during a maintenance window you choose.
Backups. RDS takes automated daily backups, and keeps transaction logs, so you can restore the database to almost any second within your retention period. Doing that yourself takes real expertise.
Failover. With the Multi-AZ option, RDS keeps a standby copy in another availability zone, and switches to it automatically if the main one fails.
Monitoring and scaling. Metrics are built in, and you can grow storage, or move to a bigger instance class, with a few clicks.
The trade-off is less control, for example no SSH access to the database server, and a higher price than raw EC2. For most teams, the time saved is worth far more than the difference.
Quick reference
- Patching: RDS applies engine updates in a maintenance window
- Backups: Automated daily backups + point-in-time restore
- Failover: Multi-AZ: a standby takes over automatically
- Monitoring + scaling: Metrics built in; resize storage and class easily
- Trade-off: Less control, and it costs more than raw EC2
RDS in six words
Six words, again. The engine is the database software. RDS supports PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, and Amazon's own Aurora. We'll use PostgreSQL, which is free, open source, and hugely popular.
The instance class is the size of the database server, like db dot t4g dot micro.
Storage is a disk, based on E B S, that you can grow later.
The endpoint is a DNS name. Your application connects to that name, never to an IP address, because the IP can change, for example after a failover. Here's the database we're about to create.
A subnet group tells RDS which subnets, and so which availability zones, it may use.
And a parameter group holds engine settings, like the maximum number of connections. The defaults are fine for now.
Quick reference
- Engine: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, Aurora
- Instance class: Size of the database server, e.g. db.t4g.micro
- Storage: EBS based, e.g. 20 GB gp3, can grow
- Endpoint: The DNS name your app connects to
- Subnet group: Which subnets (AZs) it may live in
- Parameter group: Engine settings, like max connections
our database
Identifier: app-db Engine: PostgreSQL Template: Free tier Class: db.t4g.micro Storage: 20 GB Public: No Port: 5432 Access: only from sg-web
Step 1 · Create the database
Step one: create the database. Open the RDS console, choose create database, and pick standard create, so you see every option.
Choose PostgreSQL, and the free tier template. The template sets sensible defaults for learning, like a single availability zone.
Name the instance app dash db. The master user name can stay postgres.
For the password, generate something long and random, and store it in your password manager. RDS can also store and rotate it for you in Secrets Manager, which is a great option for real projects, though Secrets Manager has its own small cost.
Keep a small, free tier eligible instance class, and twenty gigabytes of storage.
Now the important part. Under connectivity, choose connect to an EC2 compute resource, and select web one, the server from episode four. RDS then creates the security group rules for you, so only that server can reach the database.
And set public access to no. Your database will have no public address at all. Add your project tag, and create it. It takes a few minutes, so let's look at what just happened behind the scenes.
Quick reference
- RDS → Create database → Standard create
- Engine: PostgreSQL · Template: Free tier
- Identifier: app-db · master user: postgres
- Password: long + random, save it in a password manager
- Class: db.t4g.micro or db.t3.micro · 20 GB
- Connectivity: Connect to an EC2 compute resource → web-1
- Public access: No · Tag project = cloud-course
How the database stays private
Here's the security design, and it's a pattern you'll use in every architecture.
First, the database has no public IP. Nothing on the internet can even try to connect to it.
Second, our web server sits in the security group s g web.
Third, the database has its own security group, with one inbound rule: port five four three two, allowed only from the security group s g web.
So the database accepts connections only from servers in that group. Add a second web server to s g web later, and it can connect automatically. Remove it, and it can't.
That's the key idea: allow by security group, not by IP address. IP addresses change, and a rule that references another security group always describes your intent exactly.
Quick reference
Internet → EC2: web-1 → DB SG → RDS: app-db
Allow by security group, not by IP address
Step 2 · Connect from EC2
Step two: connect. Once the status says available, SSH into web one.
Install the PostgreSQL client tools. We only need the client, not the database server itself.
Then connect with psql, using dash h with the database endpoint, dash u with the user name, and the default postgres database. Enter your password when asked.
You're connected. Create a database for your application, called app d b.
Backslash l lists databases, and backslash q quits.
Now try the same command from your own laptop. It simply times out. There's no public route to the database, which is exactly what we want.
You'll find the endpoint in the RDS console, under the database's connectivity and security tab.
Quick reference
ssh -i ~/.ssh/web-key.pem ubuntu@<web-1 public IP> sudo apt update && sudo apt install -y postgresql-client psql -h app-db.abc123xyz.<region>.rds.amazonaws.com -U postgres -d postgres postgres=> CREATE DATABASE appdb; postgres=> \l -- list databases \q -- quit
Step 3 · Credentials, the right way
Step three: credentials. This is where many real projects go wrong.
Never hard-code the database password in your application's code, and never commit it to Git, even in a private repository.
Store it in AWS Secrets Manager, which can also rotate it automatically, or in Systems Manager Parameter Store, which has a free standard tier and is great for learning.
Give the EC2 server an I A M role that's allowed to read that one secret. No keys stored on the server at all. This is exactly the roles idea from episode three.
Most frameworks, including Laravel, Django and Node apps, can read a single connection setting, often called database URL. Your startup script fetches the secret, and sets it.
And create a separate database user for your application, with only the permissions it needs, rather than using the all-powerful master user. Least privilege applies inside the database too.
Quick reference
- Never hard-code passwords in code or Git
- Store them in Secrets Manager or SSM Parameter Store
- Give the EC2 server an IAM role to read that secret
- App reads one setting at startup: DATABASE_URL
- Use a separate app user, not the master user
what your app sees
DATABASE_URL=postgresql://app_user: ••••••@app-db.abc123xyz.<region> .rds.amazonaws.com:5432/appdb stored in: Secrets Manager read by: EC2 role, at startup
Backups and high availability
Now, the features that make RDS worth it.
Automated backups. RDS takes a daily snapshot, and keeps transaction logs, for a retention period you choose, up to thirty five days.
That enables point in time restore. Someone deleted a table at two fifteen? Restore to two fourteen. Note that a restore always creates a new database instance, with a new endpoint, so you then point your app at it. Practice this once, before you need it for real.
Manual snapshots are kept until you delete them. Take one before any risky change, like a big migration.
Multi-AZ keeps a synchronized standby in another availability zone. If the primary fails, or during some maintenance, RDS switches over automatically, usually within a minute or two, and the endpoint stays the same. That's why we always connect by name.
And read replicas are extra copies that serve read queries, useful for busy applications and for reporting, so heavy reports don't slow down your main database.
Quick reference
- Automated backups: Daily snapshot + logs. Retention up to 35 days
- Point-in-time restore: Restore to a second, as a NEW instance
- Manual snapshots: Kept until you delete them. Before big changes
- Multi-AZ: Standby in another AZ, automatic failover
- Read replicas: Copies for read-heavy apps and reporting
Which AWS database, when?
RDS isn't AWS's only database, and interviewers love asking when you'd use which. Here's a simple map.
RDS with PostgreSQL or MySQL is the right choice for most web applications. Users, orders, invoices: relational data, where you need SQL queries and joins.
Aurora is Amazon's own engine, compatible with MySQL and PostgreSQL, built for higher performance and availability at scale. It's a common upgrade when a regular RDS database grows up.
DynamoDB is a NoSQL key-value database. It's serverless, scales to enormous traffic, and you pay per request or per capacity. It's great for simple access patterns, like looking things up by an I D, but it needs a different way of designing your data.
ElastiCache is an in-memory cache, using Redis or Valkey. It sits in front of a database, and makes repeated reads extremely fast.
The rule of thumb: start with PostgreSQL on RDS. It handles far more than most people expect. Add the others when you have a real, measured need, not because they sound impressive.
Quick reference
- RDS PostgreSQL/MySQL: Most web apps. Relational data, SQL, joins
- Aurora: AWS-built, MySQL/Postgres compatible, for scale
- DynamoDB: Key-value NoSQL, serverless, huge scale
- ElastiCache: In-memory cache (Redis/Valkey) for speed
- Rule of thumb: Start with Postgres. Add others for a real need
What RDS costs, and cleaning up
Let's talk cost, because databases are one of the most common surprise bills.
You pay for every hour the instance runs, even with zero traffic. An idle database isn't free.
You also pay for storage, and for backup storage beyond what's included.
Multi-AZ roughly doubles the instance cost, because there are two servers. Skip it for learning, but use it for production data that matters.
You can stop an RDS instance for up to seven days. After that, AWS starts it again automatically, so stopping isn't a long-term solution.
To finish this lab, delete the database. RDS asks whether to keep a final snapshot. For a lab, you can skip it. For anything real, keep it. Afterwards, check the snapshots page, because manual snapshots stay, and cost money, until you delete them.
Quick reference
- Instance hours: Charged while running, even with zero traffic
- Storage + backups: Disk size, plus backups beyond the free amount
- Multi-AZ: Roughly doubles instance cost. Worth it in production
- Stop (temporarily): Up to 7 days, then it starts again by itself
- Delete: Choose: final snapshot or not. Then check snapshots
Compute, storage, and now a database
Let's recap. You learned why a managed database saves so much work.
You created a PostgreSQL database with no public access, protected by a security group that only allows your web server.
You connected from EC2 using the endpoint name.
You learned how to handle credentials properly, with Secrets Manager or Parameter Store, and an I A M role.
And you understand backups, point in time restore, Multi-AZ, and read replicas. With EC2, S3 and RDS, you now know the three building blocks of most web applications. In the next episode, we change how we package applications: Docker containers, from your laptop all the way to Amazon's container registry, E C R. The notes are linked in the description.
Quick reference
- Managed: Patching, backups and failover handled by RDS
- Private: No public access; allowed only from sg-web
- Connected: psql from EC2, using the endpoint name
- Secrets: Secrets Manager / Parameter Store + IAM role
- Recoverable: Backups, point-in-time restore, snapshots
Practice
- Create a table, insert a few rows, then restore the database to a point in time before the insert.
- Try connecting from your laptop and confirm it fails (that's the point).
- Store the password in SSM Parameter Store and read it with the CLI.
Next episode
Episode 7: Docker, from your laptop to Amazon ECR.
Key takeaways
- RDS handles patching, backups and failover: less control, much less work
- Keep the database private and allow access only from the app's security group
- Store credentials in Secrets Manager or Parameter Store, not in code
- Point-in-time restore creates a new instance; Multi-AZ adds automatic failover