|
1 | | -# Aurora DSQL pgJDBC code examples |
| 1 | +# pgJDBC with Aurora DSQL |
2 | 2 |
|
3 | 3 | ## Overview |
4 | 4 |
|
5 | | -The code examples in this topic show you how to use the Java to work with Aurora DSQL. |
| 5 | +This code example demonstrates how to use `pgJDBC` with Amazon Aurora DSQL. |
| 6 | +The example shows you how to connect to an Aurora DSQL cluster and perform basic database operations. |
6 | 7 |
|
7 | | -## Run the examples |
| 8 | +Aurora DSQL is a distributed SQL database service that provides high availability and scalability for |
| 9 | +your PostgreSQL-compatible applications. `pgJDBC` is a popular PostgreSQL adapter for Java that allows |
| 10 | +you to interact with PostgreSQL databases using Java 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 |
8 | 33 |
|
9 | 34 | ### Prerequisites |
10 | 35 |
|
11 | | -- java version >= 17 is needed |
| 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 | +* Java Development Kit (JDK): Ensure you have JDK 17+ installed. |
| 41 | + |
| 42 | + _To verify the java is installed, you can run_ |
| 43 | + ```bash |
| 44 | + java -version |
| 45 | + |
| 46 | +* Build Tool (Maven or Gradle) |
| 47 | + - _Maven_: Ensure Maven is installed if that is your preferred option. You can download it from the [official website](https://maven.apache.org/download.cgi). |
| 48 | + - _Gradle_: Ensure Gradle is installed if that is your preferred option. You can download it from the [official website](https://gradle.org/install/). |
| 49 | +* AWS SDK: Ensure that you setup the latest version of the AWS Java SDK [official website](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html) |
| 50 | +* You must have an Aurora DSQL cluster. For information about creating an Aurora DSQL cluster, see the |
| 51 | + [Getting started with Aurora DSQL](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/getting-started.html) |
| 52 | + guide. |
| 53 | +* If connecting as a non-admin user, ensure the user is linked to an IAM role and is granted access to the `myschema` |
| 54 | + schema. See the |
| 55 | + [Using database roles with IAM roles](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/using-database-and-iam-roles.html) |
| 56 | + guide. |
| 57 | + |
| 58 | +### Run the code |
| 59 | + |
| 60 | +The example demonstrates the following operations: |
| 61 | + |
| 62 | +- Opening a connection to an Aurora DSQL cluster |
| 63 | +- Creating a table |
| 64 | +- Inserting and querying data |
| 65 | + |
| 66 | +The example is designed to work with both admin and non-admin users: |
12 | 67 |
|
13 | | -### Run the example tests |
| 68 | +- When run as an admin user, it uses the `public` schema |
| 69 | +- When run as a non-admin user, it uses the `myschema` schema |
14 | 70 |
|
15 | | -```sh |
16 | | -# Use the account credentials dedicated for javascript |
17 | | -export CLUSTER_ENDPOINT="<your cluster endpoint>" |
18 | | -export REGION="<your cluster region>" |
19 | | -mvn test |
| 71 | +**Note:** running the example will use actual resources in your AWS account and may incur charges. |
| 72 | + |
| 73 | +Set environment variables for your cluster details: |
| 74 | + |
| 75 | +```bash |
| 76 | +# e.g. "admin" |
| 77 | +export CLUSTER_USER="<your user>" |
| 78 | + |
| 79 | +# e.g. "foo0bar1baz2quux3quuux4.dsql.us-east-1.on.aws" |
| 80 | +export CLUSTER_ENDPOINT="<your endpoint>" |
| 81 | +
|
| 82 | +# e.g. "us-east-1" |
| 83 | +export REGION="<your region>" |
20 | 84 | ``` |
21 | 85 |
|
| 86 | +Run the example: |
| 87 | + |
| 88 | + - _Maven_: |
| 89 | + |
| 90 | + ```bash |
| 91 | + mvn compile assembly:single |
| 92 | + java -ea -jar target/AuroraDSQLExample-1.0-SNAPSHOT-jar-with-dependencies.jar |
| 93 | + ``` |
| 94 | + |
| 95 | + - _Gradle_: |
| 96 | + |
| 97 | + ```bash |
| 98 | + gradle run |
| 99 | + ``` |
| 100 | + |
| 101 | + |
| 102 | +The example contains comments explaining the code and the operations being performed. |
| 103 | + |
| 104 | +## Additional resources |
| 105 | + |
| 106 | +* [Amazon Aurora DSQL Documentation](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html) |
| 107 | +* [pgJDBC Documentation](https://jdbc.postgresql.org/documentation/) |
| 108 | +* [AWS SDK for Java Documentation](https://docs.aws.amazon.com/sdk-for-java/) |
| 109 | + |
22 | 110 | --- |
23 | 111 |
|
24 | 112 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
25 | 113 |
|
26 | | -SPDX-License-Identifier: MIT-0 |
| 114 | +SPDX-License-Identifier: Apache-2.0 |
0 commit comments