postgres_fdw を使用する

Prev Next

VPC環境で利用できます。

Cloud DB for PostgreSQLで postgres_fdw extensionを提供していますので、以下のご利用ガイドをご参照ください。

consoleによる extensionのインストール

Cloud DB For PostgreSQLでは superuser権限を提供していないので、consoleを通じて superuser権限が必要な extensionをインストールできます。
詳細は、以下のガイドをご参照ください。

ACG設定

DBサーバ間の通信のために ACG設定が必要です。
詳細は、以下のガイドをご参照ください。

DB user管理およびアクセス制御設定

外部からアクセスしたり、Cloud DB For PostgreSQL間の円滑な通信のために、DB userに対するアクセス制御設定が必要です。
詳細は、以下のガイドをご参照ください。

Cloud DB For PostgreSQL間の通信

postgres_fdw_extension

consoleから Cloud DB for PostgreSQL > DB Serverでクラスタを選択します。
確認される private domainを nslookupするか、プライベート IPアドレスを確認します。

nslookup {private domain}

Non-authoritative answer:
名前:    {private domain}
Address:  xxx.xxx.xxx.xxx

確認された private ipを利用してアクセス制御を設定します。

postgres_fdw_db_user

postgres_fdwの使用例

上記の手続きをすべて行ったとします。

source DBの情報例

  • DB名: testdb
  • DB owner : testappo
  • private ip : 10.0.0.6
  • DBポート: 5432

target DBの情報例

  • DB名: testdb
  • DB owner : testappo

source DBでのスキーマおよびデータ作成

データを照会する remoteサーバにスキーマとテーブルを作成し、データを insertします。

create schema test;

create table test.test_table(aa int , bb text);

insert into test.test_table(aa,bb) values(1,'a');

insert into test.test_table(aa,bb) values(2,'b');

insert into test.test_table(aa,bb) values(3,'c');

insert into test.test_table(aa,bb) values(4,'d');

target DBでの postgres_fdw設定および照会

source DBにあるテーブルを照会するために、postgres_fdwの設定を行います。
以下の例の OPTIONSには、source DBに関する情報を入力します。

#### スキーマを作成します。
create schema test;

#### postgres_fdwがインストールされた DBに DB ownerにてアクセスした後、「postgres_server」という名前で FOREIGN DATA WRAPPERを作成します。
CREATE SERVER postgres_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '10.0.0.6', port '5432', dbname 'testdb');

#### 作成された FDWを確認
select * from pg_foreign_data_wrapper;

#### db user(role)の user mappingを設定
-- target DBに作成された role : testappo
-- source DBに作成された role : testappo
CREATE USER MAPPING FOR testappo SERVER postgres_server OPTIONS (user 'testappo', password '******');

#### userマッピングの作成を確認
select * from pg_user_mappings

#### foreigin tabler作成
CREATE FOREIGN TABLE test.test_table_fw
(
    aa int,
   bb text
) SERVER postgres_server OPTIONS ( schema_name 'test', table_name 'test_table');

#### foreign tableを確認
select * from pg_foreign_table;
SELECT *  FROM information_schema.foreign_tables;

#### 設定した FDWを利用して照会
select * from test.test_table_fw;