Spring Boot with Amazon RDS
Learn how to connect a Spring Boot application with Amazon RDS PostgreSQL step by step. This guide covers RDS architecture, database creation, networking, security groups, Spring Data JPA integration, CRUD APIs, connection pooling, and production best practices.
Introduction
Almost every enterprise application needs a reliable relational database.
Instead of installing and managing databases manually on EC2 servers, AWS provides Amazon RDS (Relational Database Service), a fully managed database service that handles provisioning, backups, patching, monitoring, storage scaling, and high availability.
In this article, we will build a Spring Boot application that connects to an Amazon RDS PostgreSQL database using Spring Data JPA.
By the end of this guide, you will have a production-ready Spring Boot application performing CRUD operations on an RDS database.
Learning Objectives
After completing this article, you will understand:
- What is Amazon RDS?
- RDS Architecture
- Why use RDS?
- RDS vs Self-Managed Database
- Create PostgreSQL RDS Instance
- Configure Security Groups
- Configure Database Connectivity
- Spring Boot Integration
- Spring Data JPA
- CRUD REST APIs
- Testing Database Connection
- Production Best Practices
What is Amazon RDS?
Amazon RDS (Relational Database Service) is a managed database service.
AWS manages:
- Database installation
- Patching
- Backups
- Failover
- Storage scaling
- Monitoring
Developers focus only on the application.
Supported Database Engines
Amazon RDS supports:
| Database | Supported |
|---|---|
| PostgreSQL | ✅ |
| MySQL | ✅ |
| MariaDB | ✅ |
| Oracle | ✅ |
| SQL Server | ✅ |
For Spring Boot applications, PostgreSQL is one of the most commonly used databases.
Why Amazon RDS?
Without RDS, you must manage:
- EC2 server
- Database installation
- OS updates
- Backups
- Monitoring
- Storage
- Replication
With RDS:
AWS handles all infrastructure tasks.
RDS High-Level Architecture
flowchart LR
USER[Client]
API[Spring Boot Application]
RDS[(Amazon RDS PostgreSQL)]
USER --> API
API --> RDS
Enterprise Architecture
flowchart TD
Users
ALB
SpringBoot
HikariCP
AmazonRDS
CloudWatch
Users --> ALB
ALB --> SpringBoot
SpringBoot --> HikariCP
HikariCP --> AmazonRDS
SpringBoot --> CloudWatch
RDS Components
| Component | Description |
|---|---|
| DB Instance | Database server |
| Endpoint | Database hostname |
| Security Group | Firewall |
| Storage | Database disk |
| Backup | Automatic snapshots |
| Multi-AZ | High availability |
| Read Replica | Read scaling |
RDS vs EC2 Database
| Feature | EC2 | Amazon RDS |
|---|---|---|
| Installation | Manual | Managed |
| Backups | Manual | Automatic |
| Monitoring | Manual | CloudWatch |
| Storage Scaling | Manual | Easy |
| Failover | Manual | Automatic |
| Patching | Manual | Managed |
Real-Time Use Cases
Amazon RDS is commonly used for:
- Banking systems
- Insurance platforms
- E-Commerce
- Learning Management Systems
- ERP
- CRM
- Inventory systems
- HR applications
Project Architecture
flowchart TD
Browser
SpringBoot
SpringDataJPA
Hibernate
AmazonRDS
Browser --> SpringBoot
SpringBoot --> SpringDataJPA
SpringDataJPA --> Hibernate
Hibernate --> AmazonRDS
Prerequisites
Install:
Java 17
Maven
AWS CLI
Spring Boot
PostgreSQL Driver
Verify Java:
java -version
Output:
openjdk version "17"
Verify AWS CLI:
aws --version
Step 1 Create Amazon RDS
AWS Console
↓
Amazon RDS
↓
Create Database
Choose
Standard Create
Database Engine
Choose
PostgreSQL
Version
Latest stable version
Templates
Choose
Free Tier
Learning purpose.
Production:
Production Template
Database Configuration
Database Name
codewithvenu-db
Master Username
postgres
Password
********
Instance Type
Learning
db.t3.micro
Production
db.r6g.large
or higher.
Storage
Choose
20 GB
Enable
Storage Autoscaling
Connectivity
Public Access
Learning
Yes
Production
No
Use private subnet.
Security Group
Create Security Group
Inbound Rule
PostgreSQL
Port
5432
Source
Your IP
Never allow
0.0.0.0/0
for production.
Create Database
Click
Create Database
Wait
5–10 minutes.
Verify Database
Status
Available
Database Endpoint
Example
codewithvenu-db.cwxyzabc.us-east-1.rds.amazonaws.com
Port
5432
Save this endpoint.
Test Database Connection
Using psql
psql \
-h codewithvenu-db.cwxyzabc.us-east-1.rds.amazonaws.com \
-U postgres \
-d postgres
Output
Connected
Step 2 Create Spring Boot Project
Dependencies
- Spring Web
- Spring Data JPA
- PostgreSQL Driver
- Validation
- Actuator
Project Structure
springboot-rds-demo
src
controller
entity
repository
service
dto
config
resources
application.yml
Maven Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
application.yml
spring:
datasource:
url: jdbc:postgresql://codewithvenu-db.cwxyzabc.us-east-1.rds.amazonaws.com:5432/postgres
username: postgres
password: your-password
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
format_sql: true
Create Entity
Example
@Entity
@Table(name="employees")
public class Employee {
@Id
@GeneratedValue
private Long id;
private String name;
private String department;
}
Repository
public interface EmployeeRepository
extends JpaRepository<Employee,Long>{
}
Service Layer
@Service
public class EmployeeService{
@Autowired
EmployeeRepository repository;
}
REST Controller
@RestController
@RequestMapping("/employees")
public class EmployeeController{
}
POST API
POST
/employees
Input
{
"name":"Venu",
"department":"Architecture"
}
Output
{
"id":1,
"name":"Venu",
"department":"Architecture"
}
GET API
GET
/employees
Output
[
{
"id":1,
"name":"Venu",
"department":"Architecture"
}
]
PUT API
PUT
/employees/1
Input
{
"name":"Venu Reddy",
"department":"Cloud"
}
DELETE API
DELETE
/employees/1
Output
Employee Deleted
CRUD Flow
flowchart LR
Client
Controller
Service
Repository
RDS
Client --> Controller
Controller --> Service
Service --> Repository
Repository --> RDS
Verify Database
Connect using
psql
Run
select * from employees;
Output
1
Venu
Architecture
Connection Flow
flowchart TD
SpringBoot["Spring Boot"]
DataSource["DataSource"]
HikariCP["HikariCP"]
Driver["PostgreSQL JDBC Driver"]
RDS["Amazon RDS"]
SpringBoot --> DataSource
DataSource --> HikariCP
HikariCP --> Driver
Driver --> RDS
Common Errors
Connection Refused
Check
- Security Group
- Endpoint
- Port
Password Authentication Failed
Verify
Username
Password
Timeout
Check
Public Access
Security Group
Internet Connectivity
Database Does Not Exist
Create database.
Update
spring.datasource.url
Best Practices
- Use PostgreSQL for Spring Boot applications
- Never expose RDS publicly in production
- Restrict Security Groups
- Use Spring Data JPA
- Enable connection pooling
- Enable automatic backups
- Enable Multi-AZ for production
- Monitor CloudWatch metrics
- Store credentials in AWS Secrets Manager
- Never hardcode passwords in source code
Summary
In this article, we learned how to integrate a Spring Boot application with Amazon RDS PostgreSQL.
We covered:
- Amazon RDS overview
- RDS architecture
- Creating an RDS instance
- Security Groups
- Spring Boot configuration
- Spring Data JPA integration
- CRUD APIs
- Database connectivity
- Common errors
- Best practices
In Part 2, we will implement:
- HikariCP Connection Pool Tuning
- Flyway Database Migration
- Multi-AZ Architecture
- Read Replicas
- Automatic Backups
- IAM Database Authentication
- AWS Secrets Manager
- CloudWatch Monitoring
- Performance Insights
- Production Architecture
- Interview Questions
Comments
Share a question, correction, or practical insight about this article.
Checking login status...
Loading approved comments...