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.

Create the database

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

Change the scaling config

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.

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

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.

The following are accessible through the SDK at runtime.

clusterArn

Type string

The ARN of the RDS Cluster.

database

Type string

The name of the database.

secretArn

Type string

The ARN of the master user secret.

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 => ClusterArgs | void)

Transform the RDS Cluster.

transform.instance?

Type ClusterInstanceArgs | (args: ClusterInstanceArgs => ClusterInstanceArgs | void)

Transform the database instance in the RDS Cluster.

version?

Type Input<string>

Default “15.5”

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

{
version: "13.9"
}