|
1 | | -# Aurora DSQL Ruby-pg code examples |
| 1 | +# Ruby-pg with Aurora DSQL |
2 | 2 |
|
3 | 3 | ## Overview |
4 | 4 |
|
5 | | -The code examples in this topic show you how to use the Ruby-pg work with Aurora DSQL. |
| 5 | +This code example demonstrates how to use Ruby-pg to interact with Amazon Aurora DSQL (DSQL). The example shows you how |
| 6 | +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. Ruby-pg is a popular PostgreSQL adapter for Ruby that allows |
| 10 | +you to interact with PostgreSQL databases using Ruby 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. The `myschema` schema needs to be created prior to running the example and the **non-admin user** needs to be granted access to the schema. |
| 20 | + |
| 21 | +The code automatically detects the user type and adjusts its behavior accordingly. |
| 22 | +The example contains comments explaining the code and the operations being performed. |
| 23 | + |
| 24 | +## ⚠️ Important |
| 25 | + |
| 26 | +* Running this code might result in charges to your AWS account. |
| 27 | +* We recommend that you grant your code least privilege. At most, grant only the |
| 28 | + minimum permissions required to perform the task. For more information, see |
| 29 | + [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). |
| 30 | +* This code is not tested in every AWS Region. For more information, see |
| 31 | + [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). |
| 32 | + |
| 33 | +## Run the example |
8 | 34 |
|
9 | 35 | ### Prerequisites |
10 | 36 |
|
11 | | -* Ruby version >=2.5 is needed |
12 | | -* AWS credentials file is configured |
| 37 | +* You must have an AWS account, and have your default credentials and AWS Region |
| 38 | + configured as described in the |
| 39 | + [Globally configuring AWS SDKs and tools](https://docs.aws.amazon.com/credref/latest/refdocs/creds-config-files.html) |
| 40 | + guide. |
| 41 | +* You must have an Aurora DSQL cluster. For information about creating an Aurora DSQL cluster, see the |
| 42 | + [Getting started with Aurora DSQL](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/getting-started.html) |
| 43 | + guide. |
| 44 | +* If connecting as a non-admin user, ensure the user is linked to an IAM role and is granted access to the `myschema` |
| 45 | + schema. See the |
| 46 | + [Using database roles with IAM roles](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/using-database-and-iam-roles.html) |
| 47 | + guide. |
13 | 48 |
|
14 | 49 |
|
15 | | -### Setup test running environment |
| 50 | +### Driver Dependencies |
16 | 51 |
|
17 | | -```sh |
| 52 | +Before using the Ruby-pg driver, ensure you have the following prerequisites installed: |
| 53 | +Ruby: Ensure you have ruby v3+ installed from the [official website](https://www.ruby-lang.org/en/documentation/installation/). |
18 | 54 |
|
19 | | -bundle install |
| 55 | +Verify install |
20 | 56 |
|
| 57 | +```bash |
| 58 | +ruby --version |
| 59 | +``` |
| 60 | + |
| 61 | +### Libpq library |
| 62 | + |
| 63 | +Libpq is required by Ruby-pg |
| 64 | + |
| 65 | +#### Obtaining the libpq library |
| 66 | + |
| 67 | +- It is installed with postgres installation. Therefore, if postgres is installed on the system the libpq is present in ../postgres_install_dir/lib, ../postgres_install_dir/include |
| 68 | +- It is installed when psql client program is installed, similarily as with postgres installation. |
| 69 | +- On some systems libpq can be installed through package manager e.g. |
| 70 | + - On Amazon Linux |
| 71 | + ``` |
| 72 | + sudo yum install libpq-devel |
| 73 | + ``` |
| 74 | + - On Mac libpq can be installed using brew |
| 75 | + ``` |
| 76 | + brew install libpq |
| 77 | + ``` |
| 78 | +- The [official website](https://www.postgresql.org/download/) may have a package for libpq or psql (which bundles libpq) |
| 79 | +- Ultimately, build from source which also can be obtained from [official website](https://www.postgresql.org/ftp/source/) |
| 80 | +
|
| 81 | +#### Add libpq to PATH |
| 82 | +
|
| 83 | +In some cases, it may be necessary to add the location of the libpq/bin directory to PATH |
| 84 | +
|
| 85 | +``` |
| 86 | +export PATH="$PATH:<your installed location>/libpq/bin" |
21 | 87 | ``` |
22 | 88 |
|
23 | | -### Run the example tests |
| 89 | +### Install Ruby-pg, Aurora DSQL SDK and other required dependencies |
24 | 90 |
|
25 | | -```sh |
26 | | -# Use the account credentials dedicated for ruby |
| 91 | +- All the required dependencies are present in the `Gemfile` file. To get all the required dependencies, run the following command from the directory where the `Gemfile` is present. |
27 | 92 |
|
28 | | -# Download the Amazon root certificate from the official trust store |
29 | | -# This example shows one of the available certs that can be used by the client; |
30 | | -# other certs such as AmazonRootCA2.pem, AmazonRootCA3.pem, etc. can also be used. |
| 93 | +```bash |
| 94 | +bundle install |
| 95 | +``` |
| 96 | + |
| 97 | +### Download the Amazon root certificate from the official trust store |
| 98 | + |
| 99 | +Download the Amazon root certificate from the official trust store. |
| 100 | +This example shows one of the available certs that can be used by the client. |
| 101 | +Other certs such as AmazonRootCA2.pem, AmazonRootCA3.pem, etc. can also be used. |
| 102 | + |
| 103 | +``` |
31 | 104 | wget https://www.amazontrust.com/repository/AmazonRootCA1.pem -O root.pem |
| 105 | +``` |
| 106 | + |
| 107 | +Place the root.pem file in the same directory as the hello_dsql.rb example file or modify the path to it in the example file. |
32 | 108 |
|
| 109 | +### Set the environmet variables specifying cluster endpoint, region and cluster user |
| 110 | + |
| 111 | +``` |
| 112 | +# e.g. 'admin' or a custom user |
| 113 | +export CLUSTER_USER=<your cluster user> |
| 114 | +
|
| 115 | +# e.g. "foo0bar1baz2quux3quuux4.dsql.us-east-1.on.aws" |
33 | 116 | export CLUSTER_ENDPOINT="<your cluster endpoint>" |
34 | | -export REGION="<your cluster region>" |
35 | 117 |
|
36 | | -rspec |
| 118 | +# e.g. "us-east-1" |
| 119 | +export REGION="<your cluster region>" |
| 120 | +``` |
| 121 | + |
| 122 | +### Run the example |
| 123 | + |
| 124 | +Execute the following command: |
| 125 | + |
| 126 | +``` |
| 127 | +ruby hello_dsql.rb |
37 | 128 | ``` |
38 | 129 |
|
39 | 130 | --- |
|
0 commit comments