2015-08-18 11 views
0

我希望這是一個常見的問題。我有兩個不同的計算機:如何從遠程服務器到本地PostgreSQL數據庫中導入特定列的數據?

  1. 我開發的計算機,
  2. 正在運行的遠程服務器計算機

每臺計算機都有它自己的運行PostgreSQL數據庫。這兩臺計算機在開發計算機中具有基本相同的表名和幾張表;但開發機器中的表格定義和關係已更改,以更好地處理引用完整性並符合3NF標準。

我需要從遠程服務器的特定數據(即,列)導入到在開發計算機的特定的表(其中發展的表具有不同的結構,則遠程服務器)。

這是如何在PostgreSQL中做了什麼? (我想只是從pgdump加載表會覆蓋較新的開發表結構??)。

東西一起使用PostgreSQL 9.3(僞)

的線路:

insert into <local computer> charges (tposted, patient_recid, fileoninsurance) 
select tposted, patient_recid, fileoninsurance 
from <Remote Server> (select e.tposted, e.patient_recid, d.fileoninsurance 
    from encountertimes e 
    join daysheets d on (e.tposted = d.tposted and e.patient_recid =d.patient_recid). 

只是要清楚,表連接需要與遠程服務器表,而不是開發機來完成。

TIA

回答

1

您可以使用COPY來轉儲你想要的領域,並在其他數據庫加載它們:

COPY (
    SELECT e.tposted, e.patient_recid, d.fileoninsurance 
    FROM encountertimes e 
    JOIN daysheets d USING(tposted, patient_recid) 
) TO '/some/file'; -- or STDOUT 

... 

COPY charges (tposted, patient_recid, fileoninsurance) FROM '/some/file'; 

另外,使用foreign data wrappers,這將是非常相似的pseudosql。

您也可以使用pg_dump來轉儲表中的數據,作爲具有命名列的INSERT,因此表結構不必完全匹配(只要目標表是導出表的超集) 。

相關問題