# How to use PostgreSQL in your serverless app

**URL:** <https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409>\
**Category:** Chapter Comments\
**Created:** [June 11, 2021, 6:24pm UTC](https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409 "2021-06-11T18:24:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [June 11, 2021, 6:24pm UTC](https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409/1 "2021-06-11T18:24:23Z")

</div>

Link to chapter — [https://serverless-stack.com/examples/how-to-use-postgresql-in-your-serverless-app.html](https://serverless-stack.com/examples/how-to-use-postgresql-in-your-serverless-app.html)

---

<div class="post-metadata">

**Author:** ![David](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@David](https://discourse.sst.dev/u/David)\
**Post date:** [November 5, 2021, 4:12am UTC](https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409/2 "2021-11-05T04:12:30Z")

</div>

Trying to use Postgres RDS with SST. Added the following but now it won’t deploy with npx sst deploy. Get the following error.

```
npm i pg

var Native = require('pg-native')
  ╵ ~~~~~~~~~~~

There was a problem transpiling the Lambda handler.

```

Is SST meant to be used with RDS? If so, does Webpack or ESBuild need to be installed in order to exclude pg-native?

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [November 5, 2021, 5:01pm UTC](https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409/3 "2021-11-05T17:01:22Z")

</div>

Yeah I recall something like this was brought up in: [Slack](https://serverless-stack.slack.com/archives/C01JG3B20RY/p1624651516050900)

You’ll likely need to exclude it from bundling in esbuild: [Function | Serverless Stack (SST)](https://docs.serverless-stack.com/constructs/Function#nodemodules)

---

<div class="post-metadata">

**Author:** ![scisley](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/scisley/32/1282_2.png) [@scisley](https://discourse.sst.dev/u/scisley)\
**Post date:** [March 16, 2022, 9:02pm UTC](https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409/4 "2022-03-16T21:02:49Z")

</div>

I followed this example and I started getting billed more than I expected, about $25 for two weeks. It all seems to be coming from the “$0.045 per NAT Gateway Hour” line item on my bill, which I believe is related to the VPC needed for the Aurora DB cluster.

I’m doing sporadic development right now with no real users. While it’s not a lot of money, is there a simple way to turn off the VPCs when I’m not using them? If possible, I’d rather not lose the data in the Aurora table in the process.

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [March 18, 2022, 4:41pm UTC](https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409/5 "2022-03-18T16:41:47Z")

</div>

Yeah we are going to update this example to use the new [`RDS`](https://docs.serverless-stack.com/constructs/RDS) construct: [Update PostgresQL example to use RDS construct by Manitej66 · Pull Request #655 · AnomalyInnovations/serverless-stack-com · GitHub](https://github.com/AnomalyInnovations/serverless-stack-com/pull/655)

It removes the need to use the NAT Gateway.

---

<div class="post-metadata">

**Author:** ![scisley](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/scisley/32/1282_2.png) [@scisley](https://discourse.sst.dev/u/scisley)\
**Post date:** [March 18, 2022, 5:32pm UTC](https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409/6 "2022-03-18T17:32:58Z")

</div>

That’s awesome! You all are the best 🙂 I’ve subscribed to the pull request.

---

<div class="post-metadata">

**Author:** ![scisley](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/scisley/32/1282_2.png) [@scisley](https://discourse.sst.dev/u/scisley)\
**Post date:** [March 21, 2022, 11:00pm UTC](https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409/7 "2022-03-21T23:00:06Z")

</div>

After these instructions were updated to use the new RDS construct, I ran into one small problem. I was getting a “TypeError: sst3.RDS is not a constructor” error message.

In case anyone else runs into this problem, all it took was [updating the CLI](https://docs.serverless-stack.com/packages/cli#update) to fix the problem. Everything else went smoothly.

---

<div class="post-metadata">

**Author:** ![spongeboy](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/spongeboy/32/1320_2.png) [@spongeboy](https://discourse.sst.dev/u/spongeboy)\
**Post date:** [April 2, 2022, 8:36am UTC](https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409/8 "2022-04-02T08:36:27Z")

</div>

I’m running through the tutorial, and at the “Deploying to Production” step I’m getting the following error when I run `npx sst deploy --stage prod`

```nohighlight
 prod-tiplikeido-my-stack failed: Properties validation failed for resource ClusterMigrationFunctionC400958E with message:
#/Handler: expected maxLength: 128, actual: 134

```

I think this is referring to trying to create a Lambda function but the name is too long. I’m not seing this problem earlier, because I think “prod” stage name is longer than my “dev” stage name, and I’m guessing the one extra character is tipping it over the limit.

Any suggestions for workarounds or how to debug this deeper?

EDIT - Using `sst build --stage prod` I’m able to see that I think this is being caused by a long path to my handler. I’m not sure why Dev worked, but this looks to be unrelated to this tutorial (which is really good BTW!).

---

<div class="post-metadata">

**Author:** ![DomKalinowski](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/domkalinowski/32/1550_2.png) [@DomKalinowski](https://discourse.sst.dev/u/DomKalinowski)\
**Post date:** [August 11, 2024, 2:06pm UTC](https://discourse.sst.dev/t/how-to-use-postgresql-in-your-serverless-app/2409/9 "2024-08-11T14:06:09Z")

</div>

This example no longer works, returning:

```auto
ExampleStack Cluster/Cluster AWS::RDS::DBCluster CREATE_FAILED Resource handler returned message: "The engine mode serverless you requested is currently unavailable. (Service: Rds, Status Code: 400, Request ID: 1234)" (RequestToken: 1234, HandlerErrorCode: InvalidRequest)

```

After reading your “[Moving away from CDK](https://sst.dev/blog/moving-away-from-cdk.html)” blog post, I understand that fixing this issue won’t be worth anyone’s time.

I’ll have a look at the Ion-based setup.  
Hope this comment spared someone time before going to deep debugging it 😉
