Chapter 7: The database (RDS PostgreSQL)¶
7.1 Prepare first¶
- DB subnet group (RDS → Subnet groups → Create) with private subnets in ≥ 2 AZs.
- Security group
<project>-<env>-rds(Chapter 5.2). - Custom parameter group (RDS → Parameter groups → Create; family = your Postgres major version):
| Parameter | Value | Why |
|---|---|---|
rds.force_ssl |
1 |
Default is already 1 on PostgreSQL 15+ (0 on 14 and older). Set explicitly anyway. |
log_connections |
1 |
Successful logins become visible |
log_disconnections |
1 |
|
rds.log_retention_period |
10080 (7 days, the max) |
Default is 3 days |
log_min_duration_statement |
e.g. 1000 |
Slow queries (optional) |
7.2 Create the database (console)¶
RDS → Databases → Create database:
| Section | Choose | Watch out |
|---|---|---|
| Creation method | Standard / full configuration (not Easy create) | Easy create hides network choices |
| Engine | PostgreSQL, current supported major | |
| Templates | Production (or Dev/Test for staging) | |
| DB instance identifier | <project>-<env>-db |
Renaming later changes the endpoint hostname |
| Master username | e.g. <project>_admin (not used by the app) |
|
| Credentials management | Managed in AWS Secrets Manager (recommended by AWS) or Self managed | If managed, decide how the app gets the password (H18) |
| Storage | gp3, autoscaling on, encryption on | |
| Connectivity → Compute resource | Don't connect to an EC2 compute resource (you attach your own named SG) | "Connect to an EC2 compute resource" auto-creates SGs with generic names (ec2-rds-N/rds-ec2-N) |
| Network type / Network access type | IPv4 / VPC | Not "Internet access gateway" (H10) |
| VPC, DB subnet group | Your VPC, the private subnet group | |
| Public access | No (the default) | |
| VPC security group | Choose existing → <project>-<env>-rds; remove default |
|
| Database authentication | Password (optionally + IAM) | |
| Monitoring | Performance Insights / Enhanced monitoring as needed | |
| Additional configuration → Initial database name | <project>_<env> |
|
| → DB parameter group | Your custom group (Chapter 7.1) | |
| → Backup retention | 7–35 days | |
| → Log exports | PostgreSQL log → CloudWatch | |
| → Deletion protection | On |
Warning
Public access must be No, and the only security group must be <project>-<env>-rds. Either one wrong and the database can end up on the internet (H9). Confirm with dig/nc right after creation (7.3).
7.3 After creation¶
# From your laptop (must NOT be reachable):
dig +short <endpoint> # private IP expected
nc -zv -w5 <endpoint> 5432 # must time out
CloudWatch → Log groups → /aws/rds/instance/<id>/postgresql → set retention (e.g. 90 days).
Create the application role (connected as the master user from the app server). The app must not use the master user (R5):
CREATE ROLE <app_user> LOGIN PASSWORD '<long random>';
GRANT <app_user> TO CURRENT_USER; -- lets the master user assign ownership on RDS
ALTER DATABASE <db_name> OWNER TO <app_user>;
\c <db_name>
ALTER SCHEMA public OWNER TO <app_user>;
REVOKE ALL ON DATABASE <db_name> FROM PUBLIC;
GRANT CONNECT, TEMPORARY ON DATABASE <db_name> TO <app_user>;
For an existing database whose tables belong to the master user, loop ALTER TABLE/SEQUENCE/VIEW … OWNER TO <app_user> over public (skip sequences owned by columns; they move with their table).
On RDS the master user is not a true superuser (rds_superuser), so COPY … PROGRAM isn't available. The risk there is data theft through exposure or leaked credentials.