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 RDS Postgres.

Create the database

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

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],
vpc
});

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

app/page.tsx
import { Resource } from "sst";
import { Pool } from "pg";
const client = new Pool({
user: Resource.MyDatabase.username,
password: Resource.MyDatabase.password,
database: Resource.MyDatabase.database,
host: Resource.MyDatabase.host,
port: Resource.MyDatabase.port,
});
await client.connect();

Cost

By default this component uses a Single-AZ Deployment, On-Demand DB Instances of a db.t4g.micro at $0.016 per hour. And 20GB of General Purpose gp3 Storage at $0.115 per GB per month.

That works out to $0.016 x 24 x 30 + $0.115 x 20 or $14 per month. Adjust this for the instance type and the storage you are using.

The above are rough estimates for us-east-1, check out the RDS for PostgreSQL pricing for more details.

RDS Proxy

If you enable the proxy, it uses Provisioned instances with 2 vCPUs at $0.015 per hour.

That works out to an additional $0.015 x 2 x 24 x 30 or $22 per month.

The above are rough estimates for us-east-1, check out the RDS Proxy pricing for more details.


Constructor

new Postgres(name, args, opts?)

Parameters

PostgresArgs

database?

Type Input<string>

Default Based on the name of the current app

Name of a database that is automatically created.

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.

{
database: "acme"
}

instance?

Type Input<string>

Default “t4g.micro”

The type of instance to use for the database. Check out the supported instance types.

{
instance: "m7g.xlarge"
}

password?

Type Input<string>

Default A random password is generated.

The password of the master user.

{
password: "Passw0rd!"
}

proxy?

Type Input<boolean>

Default false

Enable RDS Proxy for the database.

{
proxy: true
}

storage?

Type Input<${number} GB | ${number} TB>

Default “20 GB”

The amount of storage to use for the database.

By default, gp3 storage volumes are used without additional provisioned IOPS. This provides a good baseline performance for most use cases.

The minimum storage size is 20 GB. And the maximum storage size is 64 TB.

{
storage: "100 GB"
}

transform?

Type Object

Transform how this component creates its underlying resources.

transform.instance?

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

Transform the database instance in the RDS Cluster.

transform.parameterGroup?

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

Transform the RDS parameter group.

transform.subnetGroup?

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

Transform the RDS subnet group.

username?

Type Input<string>

Default “postgres”

The username of the master user.

{
username: "admin"
}

version?

Type Input<string>

Default “16.4”

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

{
version: "15.8"
}

vpc

Type Input<Vpc | Object>

The VPC subnets to use for the database.

{
vpc: {
subnets: ["subnet-0db7376a7ad4db5fd ", "subnet-06fc7ee8319b2c0ce"],
}
}

Or create a Vpc component.

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

And pass it in. The database will be placed in the private subnets.

{
vpc: myVpc
}

vpc.subnets

Type Input<Input<string>[]>

A list of subnet IDs in the VPC.

Properties

database

Type Output<string>

The name of the database.

host

Type Output<string>

The host of the database.

id

Type Output<string>

The identifier of the Postgres instance.

nodes

Type Object

nodes.instance

password

Type Output<string>

The password of the master user.

port

Type Output<number>

The port of the database.

proxyId

Type Output<string>

The name of the Postgres proxy.

username

Type Output<string>

The username of the master user.

SDK

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


This is accessible through the Resource object in the SDK.

  • database string

    The name of the database.

  • host string

    The host of the database.

  • password string

    The password of the master user.

  • port number

    The port of the database.

  • username string

    The username of the master user.

Methods

static get

Postgres.get(name, args, opts?)

Parameters

Returns Output<Postgres>

Reference an existing Postgres database with the given name. This is useful when you create a Postgres database in one stage and want to share it in another. It avoids having to create a new Postgres database in the other stage.

Imagine you create a database in the dev stage. And in your personal stage frank, instead of creating a new database, you want to share the same database from dev.

sst.config.ts
const database = $app.stage === "frank"
? sst.aws.Postgres.get("MyDatabase", {
id: "app-dev-mydatabase",
proxyId: "app-dev-mydatabase-proxy",
})
: new sst.aws.Postgres("MyDatabase", {
proxy: true,
});

Here app-dev-mydatabase is the ID of the database, and app-dev-mydatabase-proxy is the ID of the proxy created in the dev stage. You can find these by outputting the database ID and proxy ID in the dev stage.

sst.config.ts
return {
id: database.id,
proxyId: database.proxyId,
};

PostgresGetArgs

id

Type Input<string>

The ID of the database.

proxyId?

Type Input<string>

The ID of the proxy.