2016-11-18 32 views
0

一個必須執行的查詢很簡單,只要在下面 -如何使用postgres dblink更新另一個數據庫中的表?

Update employee set is_done=true; 

,我要更新的表僅在另一個數據庫中存在。

我一直在使用這些類型的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; 

如何更新員工表的字段,該字段位於另一個數據庫上?

我也想知道,如果我們能夠實現在做類似的方式刪除,以及 - 刪除整行對於給定的ID

而且,如果我不得不做與當前數據庫表的連接在更新查詢中?

+0

後,外國數據包裝會是一個更好的解決方案 –

+0

@a_horse_with_no_name能否請您提供一個例子,由於 – Tisha

+0

https://www.postgresql.org/docs/current底部的/static/contrib-dblink-exec.html示例 –

回答

1
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你 「包裝」 QRY併發送。所以「加入」包裝查詢的唯一方法是在DO塊中的動態SQL。這將是非常醜陋的。海外商品會有創造FOREIGN 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

所以你創建服務器,地圖用戶,並創建一個外部表。

完成更新它,如果它是本地

+0

謝謝。我想這也可以。你還可以在我的描述中編輯你最後一個問題的答案: – Tisha

+0

我正在使用9.5.2 – Tisha

+0

好吧,9.6會允許你做遠程連接。關於你最後一個問題 - 你的意思是你想用CTE加入進行更新? –

0

這對我有效。

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