Postgresql 如何基于另一个数据库中具有相同表的表更新数据库中的表记录?

Postgresql 如何基于另一个数据库中具有相同表的表更新数据库中的表记录?,postgresql,cross-database,Postgresql,Cross Database,假设我有db1和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_

假设我有
db1
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中克服这些问题?

步骤如下:

步骤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步:创建与另一个数据库结构相同的外部表

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\u table\u name
,就像使用local table一样。它将在远程数据库上执行所有操作

您的更新查询可以按如下方式编写:

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

创建外部表fperson(somecolumns)服务器迁移1选项(模式名称“public”,表名称“person”);如前所述,我创建了一个外部表,现在没有从中读取的权限。外部表fpersonI的权限被拒绝。我与服务器映射到ORRY的用户一起登录。外部表是由postgres用户创建的。已将其删除,并尝试为应将其映射到的用户创建它。而且它也缺乏这样做的许可。