Nov 18, 20267 min read

RDS vs Self-Managed Postgres on EC2: Cost, Control and Operations

What Amazon RDS for PostgreSQL does for you, what you take on when you run Postgres on EC2 yourself, and when each one is the right call.

Illustration for RDS vs Self-Managed Postgres on EC2: Cost, Control and Operations

Amazon RDS for PostgreSQL and Postgres on an EC2 instance run the same database engine. The difference is who does the work around it. RDS takes backups, ships WAL for point-in-time recovery, fails over to a standby in another Availability Zone and applies minor version patches. On EC2 you get the whole machine, including superuser and the file system, and every one of those jobs becomes yours.

That makes the choice a question about control versus operations, not performance. The same instance size runs the same queries at about the same speed on both. What differs is which extensions you can load, how quickly you recover from a lost instance, who tests the restore, and how much of the bill is the instance versus the people who keep it running.

RDS vs Postgres on EC2 at a glance

Amazon RDS for PostgreSQLPostgres on EC2
BackupsAutomated, retention of 0 to 35 days, point-in-time restoreYou set up pgBackRest, WAL-G or similar
High availabilityMulti-AZ standby, failover typically 60 to 120 secondsYou build replication and failover, for example with Patroni
UpgradesMinor versions automated, major versions through RDSYou install packages and run pg_upgrade
Accessrds_superuser, no OS accessFull superuser and root
ExtensionsThe RDS supported listAnything you can install
CostPremium over the same EC2 instance, plus the standbyThe instance, its storage and your team's time

When is RDS the better fit?

RDS fits when the database is a dependency rather than the product. Automated backups are on by default with a seven-day retention you can raise to 35 days, and any second in that window can be restored to a new instance. A Multi-AZ deployment keeps a synchronous standby in another Availability Zone and fails over to it, typically in 60 to 120 seconds, behind the same DNS name.

The trade is control. The master user gets rds_superuser, not superuser, extensions come from the supported list, and settings go through parameter groups. You cannot SSH in to look at a full disk or run a tool that needs the data directory.

When is Postgres on EC2 the better fit?

EC2 fits when you need what RDS does not allow: a C extension that is not on the list, a custom build, a file system layout tuned for your workload, or tools that read the data directory directly. It can also be cheaper at scale for a team that already runs Postgres well, since the instance costs less than the same class on RDS and the expertise is already paid for.

Everything RDS does is then a system you build and test: WAL archiving and base backups to S3, a restore you rehearse, streaming replication to a standby, failover that does not split the brain, OS and Postgres patching, disk monitoring, and connection limits. The restore is the part teams skip, and it is the part that decides whether a lost volume costs minutes or days.

How do you get Postgres on AWS with Encore?

Encore

Encore is a backend framework with built-in infrastructure, and it uses infrastructure from code for the database: you declare it in the service that uses it, and Encore provisions RDS for PostgreSQL or Aurora in your own AWS account. A new RDS instance starts with Multi-AZ enabled, 7-day backup retention, encryption at rest and deletion protection, and each service gets its own database role with access to only the databases it uses.

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 accounts --example=hello-world$ cd accounts && mkdir -p accounts/migrations

Declare the database

The migration is plain SQL, and extensions in the local image, such as citext, are available with CREATE EXTENSION:

CREATE EXTENSION IF NOT EXISTS citext; CREATE TABLE accounts ( id BIGSERIAL PRIMARY KEY, email CITEXT NOT NULL UNIQUE, plan TEXT NOT NULL DEFAULT 'free', created_at TIMESTAMPTZ NOT NULL DEFAULT now() );

The database declaration and the endpoint that uses it live in the same package:

// Service accounts stores customer accounts. package accounts import ( "context" "encore.dev/storage/sqldb" ) // This line is the whole database definition. Locally it is a Postgres // container; on AWS it is a logical database on the environment's RDS instance. var db = sqldb.NewDatabase("accounts", sqldb.DatabaseConfig{ Migrations: "./migrations", }) type Account struct { ID int64 `json:"id"` Email string `json:"email"` Plan string `json:"plan"` } type SignupParams struct { Email string `json:"email"` } //encore:api public method=POST path=/accounts func Signup(ctx context.Context, p *SignupParams) (*Account, error) { a := &Account{} err := db.QueryRow(ctx, ` INSERT INTO accounts (email) VALUES ($1) RETURNING id, email, plan`, p.Email).Scan(&a.ID, &a.Email, &a.Plan) return a, err }

encore check compiled this app. Our build machine has no Docker, which the local database needs, so we ran the migration on Postgres 17: A@x.io was inserted, and a@X.io was rejected by the unique index because citext compares case-insensitively.

Instance class, storage, Multi-AZ, backup retention and the Postgres version are environment settings you can change in the Platform Dashboard, and an existing instance can be imported instead of creating a new one. The code does not change between local, staging and production, which also means a coding agent that adds a table writes a migration file rather than touching the RDS instance.

Best fit: Applications that want RDS's backups and failover without writing the Terraform, IAM and security groups around it, in their own AWS account.

RDS and Postgres on EC2 by hand

Creating the RDS instance yourself is one command, plus the subnet group, security group and parameter group it refers to:

~
aws rds create-db-instance --db-instance-identifier app-prod --engine postgres --engine-version 17.6 --db-instance-class db.m7g.large --allocated-storage 100 --storage-type gp3 --multi-az --backup-retention-period 14 --master-username app_admin --manage-master-user-password --storage-encrypted --deletion-protection --db-subnet-group-name app-private --vpc-security-group-ids sg-0123456789abcdef0

On EC2, backups are the first job. pgBackRest stores encrypted base backups and archived WAL in S3, using the instance role for credentials:

# /etc/pgbackrest/pgbackrest.conf [global] repo1-type=s3 repo1-s3-bucket=acme-pg-backups repo1-s3-endpoint=s3.eu-central-1.amazonaws.com repo1-s3-region=eu-central-1 # Use the EC2 instance role instead of stored keys. repo1-s3-key-type=auto repo1-path=/main repo1-retention-full=2 repo1-cipher-type=aes-256-cbc repo1-cipher-pass=<from your secret store> archive-async=y spool-path=/var/spool/pgbackrest start-fast=y [main] pg1-path=/var/lib/postgresql/17/main

Postgres then has to hand every WAL segment to pgBackRest:

# postgresql.conf: ship every WAL segment, which is what makes point-in-time recovery possible. archive_mode = on archive_command = 'pgbackrest --stanza=main archive-push %p' wal_level = replica

Create the stanza, check that archiving works, and schedule backups:

~
$ sudo -u postgres pgbackrest --stanza=main stanza-create$ sudo -u postgres pgbackrest --stanza=main check# Then schedule backups, for example a weekly full and a daily differential:# 0 1 * * 0 pgbackrest --stanza=main --type=full backup# 0 1 * * 1-6 pgbackrest --stanza=main --type=diff backup

That covers backups. A standby with streaming replication, automated failover with a tool such as Patroni, OS and Postgres patching, and a restore drill on a schedule are still to build, and each one has to be repeated for staging.

Best fit: Teams with Postgres operations experience who need superuser, unsupported extensions or OS-level control.

How to choose

Default to RDS unless you can name the RDS limit that blocks you, such as a missing extension or superuser access. Choose EC2 when that limit is real and you have the people to own backups, failover and patching, and rehearse the restore before you rely on it. Pick Encore when you want RDS provisioned from the database your code declares, with Multi-AZ and backups on by default, in your own AWS account.

Frequently asked questions

Is RDS better than running Postgres on EC2?

Yes. It is a solid default when you want automated backups with point-in-time recovery, Multi-AZ failover, minor version patching and monitoring from the service. Postgres on EC2 makes sense when you need superuser access, an extension RDS does not support, OS-level tuning, or when you have the people to run backups, replication and failover yourself.

Is Postgres on EC2 cheaper than RDS?

Yes. The EC2 instance is often billed at a lower rate than the equivalent RDS instance, but the comparison changes once you add a standby for high availability, storage for WAL archives and backups, and the engineering time to build and test restores and failover. Compare the total, not the hourly rate.

Can you get superuser access on RDS?

No. RDS gives the master user the rds_superuser role, which can create extensions from the supported list and manage roles, but not do everything a true superuser can, such as loading arbitrary C extensions or reading files on the server.

How does Encore set up Postgres on AWS?

You declare a database in code, and Encore provisions an RDS for PostgreSQL instance, or Aurora if you choose it, in your own AWS account. New RDS instances start with Multi-AZ enabled, 7-day backup retention and encryption at rest, and each service gets its own database role.

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