如何使用 postgres dblink 更新另一个数据库中的 table?

How can you do an update to a table in another database using postgres dblink?

必须执行的查询很简单 -

Update employee set is_done=true; 

我要更新的 table 仅存在于另一个数据库中。

我一直在使用这些类型的 dblink 查询。

INSERT Into mytable select * from 
dblink('host=10.1.1.1
 user=user
 password=password
 dbname=oat', 'SELECT * from employee') tt(
     user_id integer,
     is_done boolean

 ) on conflict(user_id) do nothing;

如何更新位于另一个数据库中的员工 table 的字段?

我也想知道我们是否可以以类似的方式实现删除 - 删除给定 ID 的整行

此外,如果我必须在更新查询中与当前数据库 table 进行连接怎么办?

这对我有用。

 select * from dblink('host=10.1.1.1
     user=user
     password=password
     dbname=oat','Update employee set is_done =true' ) tt(
     updated text);
SELECT dblink_connect('host=10.1.1.1
 user=user
 password=password
 dbname=oat');

SELECT dblink_exec('Update employee set is_done=true');

而且我建议您也使用 FDW,特别是如果您使用的是 9.6

更新

对于 dblink,您 "wrap" 请发送。因此 "join" 包装查询的唯一方法是 DO 块中的动态 SQL。这将是非常丑陋的。考虑创建 FOREIGN TABLE - 它将允许您从本地 table 轻松更新

更新二

https://www.postgresql.org/docs/current/static/sql-createserver.html https://www.postgresql.org/docs/current/static/sql-createusermapping.html https://www.postgresql.org/docs/current/static/sql-createforeigntable.html

所以你创建服务器,映射用户并创建一个外国table。

完成更新后,就好像它是本地的一样