Amazon Redshift Serverless から Aurora Serverless PostgreSQL 互換バージョンにフェデレーテッドクエリしてみた

記事タイトルとURLをコピーする


コーヒーが好きな木谷映見です。

Amazon Redshift はフェデレーテッドクエリをサポートしています。
Redshift でのフェデレーテッドクエリの概要については以下のブログをご参照ください。

blog.serverworks.co.jp

今回は、Amazon Redshift Serverless から Aurora Serverless PostgreSQL 互換バージョンへのフェデレーテッドクエリを検証してみましたので、実施手順を記載します。
私自身 PostgreSQL 初心者であるため、備忘も兼ねて詳細な実行コマンドまで記載しており手順が長くなっております。ご了承ください。

実施手順概要

東京リージョンの Amazon VPC 内に Aurora Serverless PostgreSQL 互換エディションと Redshift Serverless を構築し、Redshift Serverless からフェデレーテッドクエリを実行します。
Aurora Serverless PostgreSQL 互換エディションと Redshift Serverless に接続して操作を行うためのクライアントとして、パブリックサブネットに EC2 インスタンスを 1 台作成し、セッションマネージャーで手元の端末から接続できるようにしておきます。
Redshift Serverless から Aurora Serverless PostgreSQL 互換エディションへ接続するための認証情報は AWS Secrets Manager に保存します。

VPC の作成手順は割愛します。図を参照いただけますと幸いです。

AWS 検証環境の準備

EC2 インスタンスを構築し、psql をインストールする

以下のような EC2 インスタンスを作成します。

設定項目 パラメータ 備考
名前とタグ client 任意の名前
AMI ID ami-078296f82eb463377 Amazon Linux 2 Kernel 5.10 AMI 2.0.20220912.1 x86_64 HVM gp2
セキュリティグループ client-sg HTTP ポート 80 マイ IP からのインバウンド許可
IAM インスタンスプロフィール AmazonSSMManagedInstanceCore 権限がある IAM ロールを付与 セッションマネージャーでログインするため

EC2 インスタンスが起動したら、セッションマネージャーでログインします。
Amazon Redshift Serverless への接続確認用に、psql が使用できるようにしておきます。以下コマンドを実行して psql をインストールします。

sudo yum install postgresql

実行結果

sh-4.2$ sudo yum install postgresql
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                                                                                                                | 3.7 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package postgresql.x86_64 0:9.2.24-8.amzn2 will be installed
--> Processing Dependency: postgresql-libs(x86-64) = 9.2.24-8.amzn2 for package: postgresql-9.2.24-8.amzn2.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: postgresql-9.2.24-8.amzn2.x86_64
--> Running transaction check
---> Package postgresql-libs.x86_64 0:9.2.24-8.amzn2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================================================
 Package                                               Arch                                         Version                                               Repository                                        Size
=================================================================================================================================================================================================================
Installing:
 postgresql                                            x86_64                                       9.2.24-8.amzn2                                        amzn2-core                                       3.0 M
Installing for dependencies:
 postgresql-libs                                       x86_64                                       9.2.24-8.amzn2                                        amzn2-core                                       232 k

Transaction Summary
=================================================================================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 3.3 M
Installed size: 17 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): postgresql-libs-9.2.24-8.amzn2.x86_64.rpm                                                                                                                                          | 232 kB  00:00:00
(2/2): postgresql-9.2.24-8.amzn2.x86_64.rpm                                                                                                                                               | 3.0 MB  00:00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                             15 MB/s | 3.3 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : postgresql-libs-9.2.24-8.amzn2.x86_64                                                                                                                                                         1/2
  Installing : postgresql-9.2.24-8.amzn2.x86_64                                                                                                                                                              2/2
  Verifying  : postgresql-9.2.24-8.amzn2.x86_64                                                                                                                                                              1/2
  Verifying  : postgresql-libs-9.2.24-8.amzn2.x86_64                                                                                                                                                         2/2

Installed:
  postgresql.x86_64 0:9.2.24-8.amzn2

Dependency Installed:
  postgresql-libs.x86_64 0:9.2.24-8.amzn2

Complete!
sh-4.2$

psql とは PostgreSQL の管理ツールの一つで、対話的にデータベースやテーブルを作成したりデータの追加や取得操作することができるコマンドライン型のツールです。

Redshift Serverless を作成する

以下のような Redshift Serverless を作成します。記載していない設定はデフォルトのままです。

設定項目 パラメータ 備考
名前空間の名前 2022mmdd-namespace 任意の名前
管理者ユーザー名 admin
管理者パスワード ************
IAM ロール yyyyMMdd-Redshift-ServerlessRole ※後述
ワークグループの名前 2022mmdd-workgroup 任意の名前
セキュリティグループ redshift-serverless-sg Redshift ポート 5439 client-sg からのインバウンド許可
VPC、サブネット 3 つの AZ にまたがる 3 つのサブネットを選択する 図を参照

※ IAM ロールには信頼ポリシーのみ付与しておいてください。

  • 信頼ポリシードキュメントで設定する「信頼されているエンティティ」
    • "redshift.amazonaws.com"
    • "redshift-serverless.amazonaws.com"

信頼関係タブの信頼されたエンティティはこうなります。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "redshift.amazonaws.com",
                    "redshift-serverless.amazonaws.com"
                ]
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

実はフェデレーテッドクエリのために必要な許可は Secrets Manager へのアクセス許可だけなので、今は ID ベースの許可ポリシーは設定しなくても大丈夫です。
この IAM ロールはこの後 AWS Secret Manager の設定時に編集します。

Aurora Serverless PostgreSQL 互換エディションを作成する

DB サブネットグループをあらかじめ作成しておきます。

設定項目 パラメータ 備考
DB サブネットグループ 今回は Redshift Servreless に合わせて 3 つの AZ にまたがる 3 つのサブネットを選択する 図を参照

以下のような Aurora Serverless PostgreSQL 互換エディションを作成します。記載していない設定はデフォルトのままです。

設定項目 パラメータ 備考
エンジンのタイプ Amazon Aurora
エディション Amazon Aurora PostgreSQL 互換エディション
エンジンバージョン Aurora PostgreSQL (compatible with PostgreSQL 13.7) 「フィルターの非表示」で「Serverless v2 をサポートするバージョンを表示」にチェックすると出てくる
テンプレート 開発/テスト
DB クラスター識別子 database-2
マスターユーザー名 postgres
マスターパスワード ************
DB インスタンスクラス サーバーレス 容量の範囲 2 ~ 16 ACU
マルチ AZ 配置 Aurora レプリカを使用しない
VPC、サブネット 作成しておいた VPC と DB サブネットグループを選択 図を参照
セキュリティグループ aurora-postgresql-sg PostgreSQL ポート 5432 client-sg からのインバウンド許可、PostgreSQL ポート 5432 redshift-serverless-sg からのインバウンド許可
AZ 今回は ap-northeast-1c 任意の AZ

セキュリティグループの確認

ここまでで、セキュリティグループの設定は以下のようになっています。

AWS Secrets Manager で Aurora Serverless PostgreSQL データベース用のシークレットを設定

フェデレーテッドクエリを使用するためのシークレットと IAM ロールの作成 を参考に、Secrets Manager で Aurora Serverless PostgreSQL データベース用のシークレットを設定します。
シークレットを設定したら、Redshift Serverless が Secrets Manager に保存されたシークレットにアクセスできるよう、 Redshift Serverless に付与している IAM ロールに権限を追加します。

AWS Secrets Manager で Aurora Serverless PostgreSQL データベース用のシークレットを以下のように設定します。記載していない設定はデフォルトのままです。

設定項目 パラメータ 備考
シークレットのタイプ Amazon RDS データベースの認証情報
ユーザー名 postgres
パスワード ************
暗号化キー aws/seretsmanager
データベース database-2
シークレットの名前 dev/Redshift-FederatedQuery/AuroraPostgreSQL 任意の名前

Secrets Manager で Aurora Serverless PostgreSQL データベース用のシークレットが作成できたら、シークレットの ARN をコピーして控えておきます。

IAM マネジメントコンソールに移動し、以下の JSON を使用して IAM カスタム管理ポリシーを作成します。"Sid": "AccessSecret" で始まる上部のポリシーの "Resource" に、控えておいたシークレットの ARN を貼り付けます。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AccessSecret",
            "Effect": "Allow",
            "Action": [
                "secretsmanager:GetResourcePolicy",
                "secretsmanager:GetSecretValue",
                "secretsmanager:DescribeSecret",
                "secretsmanager:ListSecretVersionIds"
            ],
            "Resource": "arn:aws:secretsmanager:ap-northeast-1:xxxxxxxxxxxx:secret:dev/Redshift-FederatedQuery/Aurorapostgresql-Y9Ih4g"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "secretsmanager:GetRandomPassword",
                "secretsmanager:ListSecrets"
            ],
            "Resource": "*"
        }
    ]
}

IAM ポリシーが作成できたら、Redshift Serverless に付与されている IAM ロールに、作成した IAM ポリシーをアタッチします。私は Redshift Serverless を作成する で作成した IAM ロール yyyyMMdd-Redshift-ServerlessRole に、作成した IAM カスタム管理ポリシーをアタッチします。

注意:Redshift Serverless で拡張された VPC のルーティングを有効にしている場合

Redshift Serverless の設定で「拡張された VPC のルーティング」を有効にしている場合、Secrets Manager からシークレットを取得するために VPC エンドポイントが必要です。
Secrets Manager 用 VPC エンドポイントの設定例を以下に記載しておきます。

設定項目 パラメータ 備考
名前タグ secrets-manager-endp 任意の名前
サービスカテゴリ AWS のサービス
サービス com.amazonaws.ap-northeast-1.secretsmanager
VPC 今回 Redshift Serverless を作成している VPC を選択
サブネット 今回 Redshift Serverless を作成しているサブネット 3 つを選択 インターフェイス型 VPC エンドポイントは AZ障害に備え冗長化しておくことが推奨される
セキュリティグループ secrets-manager-endp-sg HTTP ポート 443 redshift-serverlss-sg からのインバウンド許可
ポリシー フルアクセス 今回はセキュリティグループで通信制御するため、エンドポイントポリシーは制限しない

拡張された VPC のルーティングについての詳細は、以下の記事をご参照ください。 blog.serverworks.co.jp

フェデレーテッドクエリをするための準備

AWS 検証環境の準備が完了したので、Redshift Serverless と Aurora Serverless にログインしてテーブルやスキーマを作成していきます。

Aurora Serverless PostgreSQL でサンプルデータベースを作成

これからコマンドを用いて、Aurora Serverless PostgreSQL で以下のように設定していきます。

設定項目 パラメータ 備考
サンプルデータベース名 aurorapostgresql2
スキーマ名 users
  • age_table
id name age
1 aoki 20
2 iida 21
3 usui 22
  • job_table
id job
1 engineer
2 musician
3 driver

psql をインストールしておいた EC2 インスタンスから Aurora Serverless PostgreSQL に以下ログインコマンドでログインします。

  • 構文
psql --host=<Aurora Serverless PostgreSQL クラスターエンドポイント or リーダーエンドポイント> --port=5432 --username=postgres --password
psql --host=database-2.cluster-cno8jxqxarf4.ap-northeast-1.rds.amazonaws.com --port=5432 --username=postgres --password
  • ログイン後の表示
sh-4.2$ psql --host=database-2.cluster-cno8jxqxarf4.ap-northeast-1.rds.amazonaws.com --port=5432 --username=postgres --password
Password for user postgres:
psql (9.2.24, server 13.7)
WARNING: psql version 9.2, server version 13.0.
         Some psql features might not work.
SSL connection (cipher: ECDHE-RSA-AES128-GCM-SHA256, bits: 128)
Type "help" for help.

postgres=> 

Aurora PostgreSQL のデータベース aurorapostgresql2 を作成します。

  • 構文
CREATE DATABASE <データベース名>;
CREATE DATABASE aurorapostgresql2;
  • 実行結果
postgres=> CREATE DATABASE aurorapostgresql2;
CREATE DATABASE
postgres=>

\l で、存在するデータベース一覧を確認できます。

  • 実行結果
postgres=> \l
                                      List of databases
       Name        |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-------------------+----------+----------+-------------+-------------+-----------------------
 aurorapostgresql2 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres          | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 rdsadmin          | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | rdsadmin=CTc/rdsadmin
 template0         | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/rdsadmin          +
                   |          |          |             |             | rdsadmin=CTc/rdsadmin
 template1         | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                   |          |          |             |             | postgres=CTc/postgres
(5 rows)

postgres=>

データベースを選択します。

  • 構文
\c データベース名
\c aurorapostgresql2
  • 実行結果
postgres=> \c aurorapostgresql2
Password for user postgres:
psql (9.2.24, server 13.7)
WARNING: psql version 9.2, server version 13.0.
         Some psql features might not work.
SSL connection (cipher: ECDHE-RSA-AES128-GCM-SHA256, bits: 128)
You are now connected to database "aurorapostgresql2" as user "postgres".
aurorapostgresql2=>

現在のスキーマを確認します。

  • 構文
select current_schema();
  • 実行結果
aurorapostgresql2=> select current_schema();
 current_schema
----------------
 public
(1 row)

aurorapostgresql2=>

スキーマを作成します。

CREATE SCHEMA users;

\dn でスキーマ一覧を確認できます。

  • 実行結果
aurorapostgresql2=> \dn
  List of schemas
  Name  |  Owner
--------+----------
 public | postgres
 users  | postgres
(2 rows)

aurorapostgresql2=>

スキーマを変更します。

  • 構文
SET search_path = users;
  • 実行結果
aurorapostgresql2=> SET search_path = users;
SET
aurorapostgresql2=>

現在のスキーマを再度確認し、users になっていることを確認します。

テーブル age_table を作成します。

  • 構文
CREATE TABLE テーブル名
(カラム1 データ型 制約,
カラム2 データ型 制約,
カラム3 データ型 制約,
PRIMARY KEY(カラム1));
CREATE TABLE age_table
 (id char(4) not null,
 name text not null,
 age integer not null,
 PRIMARY KEY(id));

\d で作成したテーブルを確認できます。

  • 実行結果
aurorapostgresql2=> \d
           List of relations
 Schema |   Name    | Type  |  Owner
--------+-----------+-------+----------
 users  | age_table | table | postgres
(1 row)

aurorapostgresql2=>

age_table テーブルにデータを登録します。

  • 構文
INSERT INTO テーブル名
 (カラム1,カラム2,カラム3) VALUES 
 (カラム1の新データ, カラム2の新データ, カラム3の新データ);
INSERT INTO age_table
 (id,name,age)VALUES
 (1,'aoki',20),
 (2,'iida',21),
 (3,'usui',22);

select * from age_table; でテーブル内容を確認します。

  • 実行結果
aurorapostgresql2=> select * from age_table;
  id  | name | age
------+------+-----
 1    | aoki |  20
 2    | iida |  21
 3    | usui |  22
(3 rows)

aurorapostgresql2=>

同様の手順で、job_table テーブルを作成します。

CREATE TABLE job_table
 (id char(4) not null,
 job text not null,
 PRIMARY KEY(id));

job_table テーブルにデータを登録します。

INSERT INTO job_table
 (id,job)VALUES
 (1,'engineer'),
 (2,'musician'),
 (3,'driver');

select * from job_table; でテーブル内容を確認します。

  • 実行結果
aurorapostgresql2=> select * from job_table;
  id  |   job
------+----------
 1    | engineer
 2    | musician
 3    | driver
(3 rows)

aurorapostgresql2=>

Redshift Serverless でサンプルデータベースを作成

Redshift Serverless でも同様にコマンドを用いて、以下のように設定していきます。

設定項目 パラメータ 備考
サンプルデータベース名 redshiftdb
スキーマ名 users
  • food_table
id food
1 onigiri
2 tamagoyaki
3 coffee

psql をインストールしておいた EC2 インスタンスから Redshift Serverless に以下ログインコマンドでログインします。

  • 構文
psql -h <Redshift Serverless エンドポイント> -U <管理者ユーザー名> -d dev -p 5439
  • 実行結果
sh-4.2$ psql -h 20220920-workgroup.xxxxxxxxxxxx.ap-northeast-1.redshift-serverless.amazonaws.com -U admin -d dev -p 5439
Password for user admin:
psql (9.2.24, server 8.0.2)
WARNING: psql version 9.2, server version 8.0.
         Some psql features might not work.
SSL connection (cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.

dev=#

Redshift Serverless のデータベース redshiftdb を作成します。

CREATE DATABASE redshiftdb;

データベースを選択します。

\c redshiftdb

現在のスキーマを確認します。

select current_schema();

スキーマを作成します。

CREATE SCHEMA users;

\dn でスキーマ一覧を確認できます。 スキーマを変更します。

SET search_path = users;

現在のスキーマを再度確認し、users になっていることを確認します。

テーブル food_table を作成します。

CREATE TABLE food_table
 (id char(4) not null,
 food text not null,
 PRIMARY KEY(id));

food_table テーブルにデータを登録します。

INSERT INTO food_table
 (id,food)VALUES
 (1,'onigiri'),
 (2,'tamagoyaki'),
 (3,'coffee');

select * from food_table; でテーブル内容を確認します。

  • 実行結果
redshiftdb=# select * from food_table;

  id  |    food
------+------------
 1    | onigiri
 2    | tamagoyaki
 3    | coffee
(3 rows)

redshiftdb=#

外部スキーマを使用して Aurora Serverless PostgreSQL データベースに接続する

CREATE EXTERNAL SCHEMA - Amazon Redshift を参考に、現在のデータベースに新しい外部スキーマを作成します。この外部スキーマを使用して、Amazon RDS for PostgreSQL または Amazon Aurora PostgreSQL 互換エディションデータベースに接続できます。
この操作は Redshift 側でおこないます。

設定項目 パラメータ 備考
作成する外部スキーマ名 newusers
接続するサービス POSTGRES
接続先のデータベース aurorapostgresql2
接続先のスキーマ users
接続先のURL database-2.cluster-cno8jxqxarf4.ap-northeast-1.rds.amazonaws.com Aurora Serverless PostgreSQL ライターインスタンスのエンドポイント
Redshift に付与した IAM ロールの ARN arn:aws:iam::xxxxxxxxxxxx:role/service-role/yyyyMMdd-Redshift-ServerlessRole
シークレット(認証情報)の ARN arn:aws:secretsmanager:ap-northeast-1:xxxxxxxxxxxx:secret:dev/Redshift-FederatedQuery/Aurorapostgresql-Y9Ih4g
  • 構文
CREATE EXTERNAL SCHEMA [IF NOT EXISTS] local_schema_name
FROM POSTGRES
DATABASE 'federated_database_name' [SCHEMA 'schema_name']
URI 'hostname' [ PORT port_number ] 
IAM_ROLE { default | 'arn:aws:iam::<AWS アカウント-id>:role/<role-name>' }
SECRET_ARN 'ssm-secret-arn' 
CREATE EXTERNAL SCHEMA newusers
FROM POSTGRES
DATABASE 'aurorapostgresql2'
SCHEMA 'users'
URI 'database-2.cluster-cno8jxqxarf4.ap-northeast-1.rds.amazonaws.com'
PORT 5432
IAM_ROLE 'arn:aws:iam::xxxxxxxxxxxx:role/service-role/yyyyMMdd-Redshift-ServerlessRole'
SECRET_ARN 'arn:aws:secretsmanager:ap-northeast-1:xxxxxxxxxxxx:secret:dev/Redshift-FederatedQuery/Aurorapostgresql-Y9Ih4g';
  • 実行結果
redshiftdb=# CREATE EXTERNAL SCHEMA newusers
redshiftdb-# FROM POSTGRES
redshiftdb-# DATABASE 'aurorapostgresql2'
redshiftdb-# SCHEMA 'users'
redshiftdb-# URI 'database-2.cluster-cno8jxqxarf4.ap-northeast-1.rds.amazonaws.com'
redshiftdb-# PORT 5432
redshiftdb-# IAM_ROLE 'arn:aws:iam::xxxxxxxxxxxx:role/service-role/yyyyMMdd-Redshift-ServerlessRole'
redshiftdb-# SECRET_ARN 'arn:aws:secretsmanager:ap-northeast-1:xxxxxxxxxxxx:secret:dev/Redshift-FederatedQuery/Aurorapostgresql-Y9Ih4g';
CREATE SCHEMA
redshiftdb=#

SVV_EXTERNAL_SCHEMAS を使用して、外部スキーマの情報を表示します。

SELECT * FROM svv_external_schemas WHERE schemaname = 'newusers';
  • 実行結果
redshiftdb=# SELECT * FROM svv_external_schemas WHERE schemaname = 'newusers';
 esoid  | eskind | schemaname | esowner |   databasename    |
                                     esoptions

--------+--------+------------+---------+-------------------+-------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------
 128103 |      3 | newusers   |     100 | aurorapostgresql2 | {"SCHEMA":"users","URI":"database-2.cluster-cno8jxqxarf4.ap-northeast-1.rds.amazonaws.com","PORT":5432,"IAM_ROLE":"arn:aws:iam
::xxxxxxxxxxxx:role/service-role/yyyyMMdd-Redshift-ServerlessRole","SECRET_ARN":"arn:aws:secretsmanager:ap-northeast-1:xxxxxxxxxxxx:secret:dev/Redshift-FederatedQuery/Auro
rapostgresql-Y9Ih4g"}
(1 row)

redshiftdb=#

eskind は外部スキーマの外部カタログのタイプで、3 は Aurora PostgreSQL または Amazon RDS PostgreSQL へのフェデレーテッドクエリを示しています。

SVV_TABLES を使用して、作成した外部スキーマ newusers に接続し、参照できる外部テーブルを確認します。Aurora Serverless PostgreSQL 上のテーブルが参照できたら接続は成功です。

SELECT * FROM svv_tables where table_type = 'EXTERNAL TABLE' AND table_schema = 'newusers';
  • 実行結果
redshiftdb=# SELECT * FROM svv_tables where table_type = 'EXTERNAL TABLE' AND table_schema = 'newusers';
 table_catalog | table_schema | table_name |   table_type   | remarks
---------------+--------------+------------+----------------+---------
 redshiftdb    | newusers     | age_table  | EXTERNAL TABLE |
 redshiftdb    | newusers     | job_table  | EXTERNAL TABLE |
(2 rows)

redshiftdb=#

Redshift Serverless から、Aurora Serverless PostgreSQL 上のテーブルが参照できました。

フェデレーテッドクエリの実行

Aurora Serverless PostgreSQL の age_table テーブル内の行数を表示します。

SELECT count(*) FROM newusers.age_table;
  • 実行結果
redshiftdb=# SELECT count(*) FROM newusers.age_table;
 count
-------
     3
(1 row)

redshiftdb=#

Aurora Serverless PostgreSQL の age_table テーブルを表示します。

select * from newusers.age_table;
  • 実行結果
redshiftdb=# select * from newusers.age_table;
  id  | name | age
------+------+-----
 1    | aoki |  20
 2    | iida |  21
 3    | usui |  22
(3 rows)

redshiftdb=#

Aurora Serverless PostgreSQL の age_table テーブルと job_table から name, age, job を取り出して表示します。

SELECT name, age, job
  FROM newusers.age_table,newusers.job_table
  WHERE age_table.id = job_table.id;
  • 実行結果
redshiftdb=# SELECT name, age, job
FROM newusers.age_table,newusers.job_table
WHERE age_table.id = job_table.id;
 name | age |   job
------+-----+----------
 iida |  21 | musician
 usui |  22 | driver
 aoki |  20 | engineer
(3 rows)

redshiftdb=# 

Aurora Serverless PostgreSQL の age_table テーブルと Redshift Serverless の food_table から name, age, food を取り出して表示します。

SELECT name, age, food
  FROM newusers.age_table,users.food_table
  WHERE age_table.id = food_table.id;
  • 実行結果
redshiftdb=# SELECT name, age, food
redshiftdb-# FROM newusers.age_table,users.food_table
redshiftdb-# WHERE age_table.id = food_table.id;
 name | age |    food
------+-----+------------
 aoki |  20 | onigiri
 iida |  21 | tamagoyaki
 usui |  22 | coffee
(3 rows)

redshiftdb=#

Redshift Serverless のテーブルと Aurora Serverless PostgreSQL のテーブルを結合したクエリが実行できました。

参考

PostgreSQL への横串検索を使用した開始方法 - Amazon Redshift

フェデレーテッドクエリを使用するためのシークレットと IAM ロールの作成

psql ツールを使用してクラスターに接続する

横串検索の使用例 - Amazon Redshift

Amazon Redshift でフェデレーテッドデータにアクセスする際の制約事項と考慮事項 - Amazon Redshift

SVV_EXTERNAL_SCHEMAS

CREATE EXTERNAL SCHEMA - Amazon Redshift

サンプルデータベース - Amazon Redshift

サンプルデータをロードする

Amazon S3 データを Aurora PostgreSQL DB クラスターにインポートする

Amazon Redshift クエリエディタ v2 を使用したデータベースのクエリの実行

emi kitani(執筆記事の一覧)

AS部LX課。2022/2入社、コーヒーとサウナが好きです。執筆活動に興味があります。AWS認定12冠。