postgresql 利用fdw來實現(xiàn)不同數(shù)據(jù)庫之間數(shù)據(jù)互通(推薦)
最近在工作的時候 需要用數(shù)據(jù)庫A中的表a1來更新插入數(shù)據(jù)庫B中的b2表 由于數(shù)據(jù)庫都是postgres數(shù)據(jù)庫
我想到了 postgres_fdw插件
準備工作
首先需要保證這兩個數(shù)據(jù)庫之間網(wǎng) 是通的
數(shù)據(jù)庫A 名字 AA ip 10.10.2.100(虛構)端口 5432 用戶名 postgres
數(shù)據(jù)庫B 名字 BB ip 100.20.0.120(虛構)端口 6688 用戶名 postgres
一 利用fdw創(chuàng)建2個數(shù)據(jù)庫之間的連接
1.1 安裝 postgres_fdw插件
Create extension "postgres_fdw";
1.2 創(chuàng)建外部連接
無論你實在A數(shù)據(jù)庫中創(chuàng)建B數(shù)據(jù)庫的連接 ,還是在B數(shù)據(jù)庫中創(chuàng)建A數(shù)據(jù)庫的連接 從本質上是一樣的,都是為了打通2個數(shù)據(jù)庫,實現(xiàn)數(shù)據(jù)互通
1.2.1 在A數(shù)據(jù)庫創(chuàng)建b數(shù)據(jù)庫的連接
--創(chuàng)建外部服務器 -- 括號里的三個參數(shù),分別是數(shù)據(jù)庫b的ip、端口和數(shù)據(jù)庫名稱 CREATE SERVER lianjiebb FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '100.20.0.120', port '6688', dbname 'BB'); --創(chuàng)建用戶映射· -- 括號里的兩個參數(shù),分別是數(shù)據(jù)庫b數(shù)據(jù)庫的用戶名、密碼 create user mapping for postgres server lianjiebb options(user 'postgres', password '數(shù)據(jù)庫密碼');
1.2.2 在b數(shù)據(jù)庫創(chuàng)建a數(shù)據(jù)庫的連接
--創(chuàng)建外部服務器 -- 括號里的三個參數(shù),分別是數(shù)據(jù)庫a的ip、端口和數(shù)據(jù)庫名稱 CREATE SERVER lianjieAA FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '10.10.2.100', port '5432', dbname 'AA'); --創(chuàng)建用戶映射· -- 括號里的兩個參數(shù),分別是數(shù)據(jù)庫a數(shù)據(jù)庫的用戶名、密碼 create user mapping for postgres server lianjieAA options(user 'postgres', password '數(shù)據(jù)庫密碼');
1.2.3 查詢 外部鏈接
select * from pg_foreign_server;
1.3 創(chuàng)建外部表
在A數(shù)據(jù)庫創(chuàng)建b1表的外部鏈接
CREATE FOREIGN TABLE tb_fdw_b1 ( collect_time timestamp(6), id varchar(36) , 列名1 numeric(12,2) , 列名2 int4 , 列名3 timestamp(6) ) --跟b1 一模一樣的表結構就行 server lianjiebb --你創(chuàng)建的外部鏈接名字 options (table_name '時序庫的表名');
查看外部表是否有數(shù),有數(shù)就表示ok了
select * from tb_fdw_b1;
在數(shù)據(jù)庫b上創(chuàng)建a1表的外部鏈接也是一樣的
二 插入數(shù)據(jù)
2.1 往外部表tb_fdw_b1 插入表a1的數(shù)據(jù)
因為 倆個表都是一樣的表結構,可以直接插入
INSERT into tb_fdw_b1 SELECT * from a1;
因為外部表是實時的 在數(shù)據(jù)庫a中更改了 外部表tb_fdw_b1 的數(shù)據(jù) ,數(shù)據(jù)庫B中的b1表中的數(shù)據(jù)也隨之改變 達成了我們一開始的目的
更新 UPDATE 刪除DELETE 也是一樣的 在這里就不說了
三 利用定時任務來達成自動同步
每天都需要執(zhí)行顯然不符合我們的“懶人”形象 ,所以用定時任務來實現(xiàn)明天自動同步是必不可少的 我讓他每天早上8點自動執(zhí)行
3.1 利用 pgadmin實現(xiàn)定時同步(截圖)
3.1.1
3.1.2
3.1.3
3.1.4
3.2 直接上語句實現(xiàn)定時同步
DO $$ DECLARE jid integer; scid integer; BEGIN -- Creating a new job INSERT INTO pgagent.pga_job( jobjclid, jobname, jobdesc, jobhostagent, jobenabled ) VALUES ( 1::integer, '更新表b1'::text, ''::text, ''::text, true ) RETURNING jobid INTO jid; -- Steps -- Inserting a step (jobid: NULL) INSERT INTO pgagent.pga_jobstep ( jstjobid, jstname, jstenabled, jstkind, jstconnstr, jstdbname, jstonerror, jstcode, jstdesc ) VALUES ( jid, '更新表b1'::text, true, 's'::character(1), ''::text, '數(shù)據(jù)庫A'::name, 'f'::character(1), ' INSERT into tb_fdw_b1 SELECT * from a1;'::text, ''::text ) ; -- Schedules -- Inserting a schedule INSERT INTO pgagent.pga_schedule( jscjobid, jscname, jscdesc, jscenabled, jscstart, jscend, jscminutes, jschours, jscweekdays, jscmonthdays, jscmonths ) VALUES ( jid, '更新表b1'::text, ''::text, true, '2021-02-07 10:24:50 +08:00'::timestamp with time zone, '2100-02-07 10:24:52 +08:00'::timestamp with time zone, -- Minutes ARRAY[true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false]::boolean[], -- Hours ARRAY[false,false,false,false,false,false,false,false,true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false]::boolean[], -- Week days ARRAY[false,false,false,false,false,false,false]::boolean[], -- Month days ARRAY[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false]::boolean[], -- Months ARRAY[false,false,false,false,false,false,false,false,false,false,false,false]::boolean[] ) RETURNING jscid INTO scid; END $$;
到此這篇關于postgresql 利用fdw來實現(xiàn)不同數(shù)據(jù)庫之間數(shù)據(jù)互通的文章就介紹到這了,更多相關postgresql實現(xiàn)數(shù)據(jù)互通內容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。