Updated Nov 30, 20265 min read

Create an RDS Postgres Database Without Terraform

How to create a private RDS for PostgreSQL instance with the AWS CLI, which subnet group, security group, credentials and backup settings it needs, what the same setup looks like in Terraform, and how to get a database in your own AWS account from application code.

Illustration for Create an RDS Postgres Database Without Terraform

Creating an RDS for PostgreSQL instance takes one command, and a database that is safe to use takes several more: private subnets, access only from the application, backups, encrypted storage and a managed master password.

This guide goes through that with the AWS CLI, then Terraform, then application code.

What does an RDS database need?

PieceWhat to decide
Subnet groupPrivate subnets in at least two Availability Zones
Network accessA security group that allows port 5432 from the application security group only
InstanceEngine version, instance class such as db.t4g.micro, storage type and size
CredentialsA master user, with the password generated and kept in Secrets Manager
ResilienceBackup retention, Multi-AZ standby and deletion protection
SecurityEncryption at rest and no public accessibility
Application accessA database role per application and a connection string delivered as a secret

RDS can generate and rotate the master password in Secrets Manager when the instance is created with a managed password, as the RDS and Secrets Manager guide explains, and the instance has to be placed in a DB subnet group that covers two Availability Zones.

What are the options besides Terraform?

The console walks through these choices in a form, and the AWS CLI does the same in three commands. The subnet group and security group come first, and the instance references both:

~
$ aws rds create-db-subnet-group --db-subnet-group-name orders-private \ --db-subnet-group-description "Private subnets for orders" \ --subnet-ids subnet-0aaa1111 subnet-0bbb2222$ aws ec2 authorize-security-group-ingress --group-id sg-0db00000 \ --protocol tcp --port 5432 --source-group sg-0app0000$ aws rds create-db-instance --db-instance-identifier orders-prod \ --engine postgres --engine-version 18.3 --db-instance-class db.t4g.micro \ --allocated-storage 20 --storage-type gp3 --storage-encrypted \ --master-username dbadmin --manage-master-user-password \ --db-name orders --db-subnet-group-name orders-private \ --vpc-security-group-ids sg-0db00000 --no-publicly-accessible \ --backup-retention-period 7 --multi-az --deletion-protection

Each flag is documented in the create-db-instance reference, and the instance takes several minutes to become available. The application still needs a role of its own instead of the master user, a connection string in a secret and an inbound rule for every service that connects, and a second environment means running the same commands again with different names. Teams that bring their own agents still need environments for each of those databases, the system context for the roles and secrets, and the controls around who can connect, which is the part Encore takes on later in this guide.

What does the Terraform look like?

The same database is a subnet group, a security group with one ingress rule and an instance resource, with the master password managed by RDS so it never lands in state:

variable "subnet_ids" { type = list(string) } variable "vpc_id" { type = string } variable "app_security_group_id" { type = string } resource "aws_db_subnet_group" "orders" { name = "orders-private" subnet_ids = var.subnet_ids } resource "aws_security_group" "orders_db" { name = "orders-db" vpc_id = var.vpc_id ingress { from_port = 5432 to_port = 5432 protocol = "tcp" security_groups = [var.app_security_group_id] } } resource "aws_db_instance" "orders" { identifier = "orders-prod" engine = "postgres" engine_version = "18.3" instance_class = "db.t4g.micro" allocated_storage = 20 storage_type = "gp3" storage_encrypted = true db_name = "orders" username = "dbadmin" manage_master_user_password = true db_subnet_group_name = aws_db_subnet_group.orders.name vpc_security_group_ids = [aws_security_group.orders_db.id] publicly_accessible = false backup_retention_period = 7 multi_az = true deletion_protection = true skip_final_snapshot = false final_snapshot_identifier = "orders-prod-final" }

The resource arguments are listed in the aws_db_instance documentation, and the RDS with Terraform guide covers parameter groups, read replicas and upgrades in more detail.

How does Encore create the database from code?

Encore brings infrastructure from code, so a database is declared in the service that uses it, encore run starts Postgres locally, and a deploy to your own AWS account creates the RDS instance, as the AWS infrastructure docs describe. A new instance runs PostgreSQL 18.3 on db.m5.large with Multi-AZ enabled, encrypted storage, deletion protection and 7 days of backups, in private subnets that accept connections only from the application. Each service receives its own database role with access to the databases it declares, so an AI coding agent adding a table works through a migration file in the repository and never holds the master credentials.

Application code passes through Encore and infrastructure guardrails to resources provisioned in your own AWS or GCP account.

Set up the app

Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

~
$ encore app create orders-app --example=ts/hello-world$ cd orders-app && mkdir -p orders/migrations

The orders folder also needs an encore.service.ts that exports new Service("orders"). The service declares the database and two endpoints:

// orders/orders.ts import { api, APIError } from "encore.dev/api"; import { SQLDatabase } from "encore.dev/storage/sqldb"; const db = new SQLDatabase("orders", { migrations: "./migrations" }); interface Order { id: number; customer: string; cents: number; } interface CreateRequest { customer: string; cents: number; } export const create = api( { expose: true, method: "POST", path: "/orders" }, async (req: CreateRequest): Promise<Order> => { if (!req.customer.trim()) throw APIError.invalidArgument("customer is required"); if (!Number.isInteger(req.cents) || req.cents <= 0) { throw APIError.invalidArgument("cents must be a positive integer"); } const row = await db.queryRow<Order>` INSERT INTO orders (customer, cents) VALUES (${req.customer}, ${req.cents}) RETURNING id, customer, cents`; return row!; }, ); export const get = api( { expose: true, method: "GET", path: "/orders/:id" }, async ({ id }: { id: number }): Promise<Order> => { const row = await db.queryRow<Order>`SELECT id, customer, cents FROM orders WHERE id = ${id}`; if (!row) throw APIError.notFound("order not found"); return row; }, );
-- orders/migrations/1_create_orders.up.sql CREATE TABLE orders ( id BIGSERIAL PRIMARY KEY, customer TEXT NOT NULL, cents BIGINT NOT NULL CHECK (cents > 0), created_at TIMESTAMPTZ NOT NULL DEFAULT now() );

encore run applies the migration to a local Postgres and serves both endpoints, and encore db shell orders opens a psql session for a look at the data. The database docs cover migrations and queries. To deploy, follow Connect your cloud account and push, and the instance, the subnet groups, the security rules and the per-service role are created in your account. A schema change is the next numbered migration file, applied on the next deploy.

Best fit: Teams that want a private, backed-up RDS Postgres database in their own AWS account, declared next to the service and run locally with encore run.

Frequently asked questions

How do I create an RDS Postgres instance from the command line?

Create a DB subnet group from private subnets in two Availability Zones, a security group that allows port 5432 from your application, and then run aws rds create-db-instance with the postgres engine, an instance class, storage, --no-publicly-accessible and --manage-master-user-password. RDS stores the generated master password in Secrets Manager.

Should an RDS database be publicly accessible?

No. Place it in private subnets with --no-publicly-accessible and allow port 5432 only from the security group of the application that connects. Reach it from a laptop through a bastion host, Session Manager port forwarding or a VPN instead of opening it to the internet.

What does an RDS instance need besides the instance itself?

A DB subnet group, a security group, credentials for the master user, a backup retention period, encryption at rest and a decision about Multi-AZ. The application also needs its own database role and a connection string stored as a secret, which are separate steps.

How do I get an RDS database without writing the setup by hand?

Declare the database in code with Encore, run encore run to use a local Postgres and deploy to your own AWS account. Encore creates RDS in private subnets with encryption at rest, deletion protection, 7 days of backups and a separate database role for each service.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders