|
| 1 | +# Postgres.js with Aurora DSQL |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This code example demonstrates how to use `Postgres.js` with Amazon Aurora DSQL. |
| 6 | +The example shows you how to connect to an Aurora DSQL cluster and perform basic database operations. |
| 7 | + |
| 8 | +Aurora DSQL is a distributed SQL database service that provides high availability and scalability for |
| 9 | +your PostgreSQL-compatible applications. `Postgres.js` is a lightweight PostgreSQL client for Node.js that allows |
| 10 | +you to interact with PostgreSQL databases using JavaScript code. |
| 11 | + |
| 12 | +## About the code example |
| 13 | + |
| 14 | +The example demonstrates a flexible connection approach that works for both admin and non-admin users: |
| 15 | + |
| 16 | +* When connecting as an **admin user**, the example uses the `public` schema and generates an admin authentication |
| 17 | + token. |
| 18 | +* When connecting as a **non-admin user**, the example uses a custom `myschema` schema and generates a standard |
| 19 | + authentication token. |
| 20 | + |
| 21 | +The code automatically detects the user type and adjusts its behavior accordingly. |
| 22 | + |
| 23 | +## ⚠️ Important |
| 24 | + |
| 25 | +* Running this code might result in charges to your AWS account. |
| 26 | +* We recommend that you grant your code least privilege. At most, grant only the |
| 27 | + minimum permissions required to perform the task. For more information, see |
| 28 | + [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). |
| 29 | +* This code is not tested in every AWS Region. For more information, see |
| 30 | + [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). |
| 31 | + |
| 32 | +## Run the example |
| 33 | + |
| 34 | +### Prerequisites |
| 35 | + |
| 36 | +* You must have an AWS account, and have your default credentials and AWS Region |
| 37 | + configured as described in the |
| 38 | + [Globally configuring AWS SDKs and tools](https://docs.aws.amazon.com/credref/latest/refdocs/creds-config-files.html) |
| 39 | + guide. |
| 40 | +* Node.js: Ensure you have Node.js 18+ installed. |
| 41 | + |
| 42 | +```bash |
| 43 | +node --version |
| 44 | +``` |
| 45 | + |
| 46 | +It should output something similar to `v18.x` or higher. |
| 47 | + |
| 48 | +* You must have an Aurora DSQL cluster. For information about creating an Aurora DSQL cluster, see the |
| 49 | + [Getting started with Aurora DSQL](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/getting-started.html) |
| 50 | + guide. |
| 51 | +* If connecting as a non-admin user, ensure the user is linked to an IAM role and is granted access to the `myschema` |
| 52 | + schema. See the |
| 53 | + [Using database roles with IAM roles](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/using-database-and-iam-roles.html) |
| 54 | + guide. |
| 55 | + |
| 56 | +### Run the code |
| 57 | + |
| 58 | +The example demonstrates the following operations: |
| 59 | + |
| 60 | +- Opening a connection to an Aurora DSQL cluster |
| 61 | +- Creating a table |
| 62 | +- Inserting and querying data |
| 63 | + |
| 64 | +The example is designed to work with both admin and non-admin users: |
| 65 | + |
| 66 | +- When run as an admin user, it uses the `public` schema |
| 67 | +- When run as a non-admin user, it uses the `myschema` schema |
| 68 | + |
| 69 | +**Note:** running the example will use actual resources in your AWS account and may incur charges. |
| 70 | + |
| 71 | +Set environment variables for your cluster details: |
| 72 | + |
| 73 | +```bash |
| 74 | +# e.g. "admin" |
| 75 | +export CLUSTER_USER="<your user>" |
| 76 | + |
| 77 | +# e.g. "foo0bar1baz2quux3quuux4.dsql.us-east-1.on.aws" |
| 78 | +export CLUSTER_ENDPOINT="<your endpoint>" |
| 79 | + |
| 80 | +# e.g. "us-east-1" |
| 81 | +export REGION="<your region>" |
| 82 | +``` |
| 83 | + |
| 84 | +Run the example: |
| 85 | + |
| 86 | +```bash |
| 87 | +npm install |
| 88 | +npm test |
| 89 | +``` |
| 90 | + |
| 91 | +The example contains comments explaining the code and the operations being performed. |
| 92 | + |
| 93 | +### Connection pooling |
| 94 | + |
| 95 | +Postgres.js uses connection pooling by default. The maximum pool size, and maximum lifespan of connections is configurable |
| 96 | +when the client is created using the options `max` and `max_lifetime` respectively. Note that connections are created lazily only |
| 97 | +when a database call occurs, not when the client is created. See [Postgres.js documentation here](https://github.com/porsager/postgres?tab=readme-ov-file#the-connection-pool) |
| 98 | +for more information. There are no guarantees as to which connection will be used when executing a command, except within a single transaction. |
| 99 | +This means users cannot rely on commands like `SET SESSION search_path=schema` to be applied correctly across multiple database |
| 100 | +interactions. |
| 101 | + |
| 102 | +## Additional resources |
| 103 | + |
| 104 | +* [Amazon Aurora DSQL Documentation](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html) |
| 105 | +* [Postgres.js Documentation](https://github.com/porsager/postgres) |
| 106 | +* [AWS SDK for JavaScript Documentation](https://docs.aws.amazon.com/sdk-for-javascript/) |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 111 | + |
| 112 | +SPDX-License-Identifier: MIT-0 |
0 commit comments