2

db1との 2 つのデータベースがあるとdb2します。のようなコマンドを実行したい

update db2.person p2
set p2.name = p1.name
from db1.person p1
where p1.id = p2.id;

これは、MySQL では問題なく可能です。PostgreSQLでそれを達成するのは非常に困難です。

私が試したこと:

create extension postgres_fdw;

create server theservername
foreign data wrapper postgres_fdw
options(host 'localhost', dbname 'thedbname', port '5432');

create user mapping for theuser
server theservername
options(user 'theusername', password 'thepassword');

そして、ここで私は立ち往生しています。どうすればよいかわかりません。これらの問題は MySQL にはありません。PostgreSQL でそれらを克服するにはどうすればよいですか?

4

1 に答える 1

4

手順は次のとおりです。

ステップ - 1: 拡張機能を作成する

create extension postgres_fdw;

ステップ - 2: サーバーの作成

create server theservername
foreign data wrapper postgres_fdw
options(host 'localhost', dbname 'thedbname', port '5432');

ステップ - 3: サーバーの外部ユーザー マッピングを作成する

create user mapping for theuser
server theservername
options(user 'theusername', password 'thepassword');

ステップ - 4: 別の DB と同じ構造の外部テーブルを作成する

create foreign table "schema_name"."local_table_name"
(
id_ int;
...
-- field list same as foreign table in other db
)
server theservername
options(SCHEMA_NAME 'foreign_schema', TABLE_NAME 'foreign_name');

local_table_nameこれで、ローカル テーブルと同じようにクエリで使用できるようになりました。リモートデータベースですべての操作を行います。

更新クエリは次のように記述できます。

update local_table_name p2
set name = p1.name
from person p1
where p1.id = p2.id;
于 2020-10-01T10:27:52.133 に答える