Skip to content

Postgres

Reference doc for the `sst.aws.Postgres` component.

The Postgres component lets you add a Postgres database to your app using Amazon Aurora Serverless v2.

To connect to your database from your Lambda functions, you can use the AWS Data API. It does not need a persistent connection, and works over HTTP. You also don’t need a VPN to connect to it locally.

Create the database

sst.config.ts
const database = new sst.aws.Postgres("MyDatabase");

Use a VPC

sst.config.ts
const vpc = new sst.aws.Vpc("MyVpc");
const database = new sst.aws.Postgres("MyDatabase", { vpc });

Change the scaling config

sst.config.ts
new sst.aws.Postgres("MyDatabase", {
scaling: {
min: "2 ACU",
max: "128 ACU"
}
});

You can link your database to other resources, like a function or your Next.js app.

sst.config.ts
new sst.aws.Nextjs("MyWeb", {
link: [database]
});

Once linked, you can connect to it from your function code.

app/page.tsx
import { Resource } from "sst";
import { drizzle } from "drizzle-orm/aws-data-api/pg";
import { RDSDataClient } from "@aws-sdk/client-rds-data";
drizzle(new RDSDataClient({}), {
database: Resource.MyDatabase.database,
secretArn: Resource.MyDatabase.secretArn,
resourceArn: Resource.MyDatabase.clusterArn
});

Constructor

new Postgres(name, args?, opts?)

Parameters

PostgresArgs

databaseName?

Type Input<string>

Default Based on the name of the current app

Name of a database that is automatically created inside the cluster.

The name must begin with a letter and contain only lowercase letters, numbers, or underscores. By default, it takes the name of the app, and replaces the hyphens with underscores.

{
databaseName: "acme"
}

scaling?

Type Input<Object>

Default {min: “0.5 ACU”, max: “4 ACU”}

The Aurora Serverless v2 scaling config. By default, the cluster has one DB instance that is used for both writes and reads. The instance can scale from the minimum number of ACUs to the maximum number of ACUs.

An ACU or Aurora Capacity Unit is a combination of CPU and RAM. The cost of an Aurora Serverless v2 cluster is based on the ACU hours used. Additionally, you are billed for I/O and storage used by the cluster. Read more here.

Each ACU is roughly equivalent to 2 GB of memory. So pick the minimum and maximum based on the baseline and peak memory usage of your app.

scaling.max?

Type Input<${number} ACU>

Default 4 ACU

The maximum number of ACUs, ranges from 0.5 to 128, in increments of 0.5.

{
scaling: {
max: "128 ACU"
}
}

scaling.min?

Type Input<${number} ACU>

Default 0.5 ACU

The minimum number of ACUs, ranges from 0.5 to 128, in increments of 0.5.

For your production workloads, setting a minimum of 0.5 ACUs might not be a great idea due to the following reasons, you can also read more here.

  • It takes longer to scale from a low number of ACUs to a much higher number.
  • Query performance depends on the buffer cache. So if frequently accessed data cannot fit into the buffer cache, you might see uneven performance.
  • The max connections for a 0.5 ACU Postgres instance is capped at 2000.
{
scaling: {
min: "2 ACU"
}
}

transform?

Type Object

Transform how this component creates its underlying resources.

transform.cluster?

Type ClusterArgs | (args: ClusterArgs, opts: ComponentResourceOptions, name: string) => void

Transform the RDS Cluster.

transform.instance?

Type ClusterInstanceArgs | (args: ClusterInstanceArgs, opts: ComponentResourceOptions, name: string) => void

Transform the database instance in the RDS Cluster.

transform.subnetGroup?

Type SubnetGroupArgs | (args: SubnetGroupArgs, opts: ComponentResourceOptions, name: string) => void

Transform the RDS subnet group.

version?

Type Input<string>

Default “15.5”

The Postgres engine version. Check out the available versions in your region.

{
version: "13.9"
}

vpc?

Type Input<Object>

Default The default VPC in your account.

The VPC to use for the database cluster.

By default, your account has a VPC in each account. This comes with a public subnet. If you don’t specify a VPC, it’ll use the default one. This is not recommended for production.

{
vpc: {
privateSubnets: ["subnet-0db7376a7ad4db5fd ", "subnet-06fc7ee8319b2c0ce"],
securityGroups: ["sg-0399348378a4c256c"],
}
}

Or create a Vpc component.

const myVpc = new sst.aws.Vpc("MyVpc");

And pass it in.

{
vpc: myVpc
}

vpc.privateSubnets

Type Input<Input<string>[]>

A list of private subnet IDs in the VPC. The database will be placed in the private subnets.

vpc.securityGroups

Type Input<Input<string>[]>

A list of VPC security group IDs.

Properties

clusterArn

Type Output<string>

The ARN of the RDS Cluster.

database

Type Output<string>

The name of the database.

nodes

Type Object

nodes.cluster

Type Cluster

nodes.instance

secretArn

Type Output<string>

The ARN of the master user secret.

SDK

Use the SDK in your runtime to interact with your infrastructure.


This is accessible through the Resource object in the SDK.

  • clusterArn string

    The ARN of the RDS Cluster.

  • database string

    The name of the database.

  • secretArn string

    The ARN of the master user secret.

Methods

static get

Postgres.get(name, clusterName)

Parameters

  • name string

    The name of the component.
  • clusterName Input<string>

    The name of the RDS cluster.

Returns Postgres

Reference an existing Postgres cluster with the given cluster name. This is useful when you created a cluster in one stage and you want to reference it in another stage.

Imagine you created a cluster in the dev stage. And in your perosonal stage, ie. frank, instead of creating a new cluster, you want to reuse the same cluster from dev.

sst.config.ts
const database = $app.stage === "frank"
? sst.aws.Postgres.get("MyDatabase", "app-dev-mydatabase")
? new sst.aws.Postgres("MyDatabase");

Here app-dev-mydatabase is the name of the cluster created in the dev stage.