Postgresql 跨庫同步表及postgres_fdw的用法說明
postgres_fdw模塊
PostgreSQL 9.3 add postgres_fdw extension for accessing remote tables
PostgreSQL 9.3新增了一個postgres_fdw模塊, 已經(jīng)整合在源碼包中. 用于創(chuàng)建postgres外部表.
注:db_des為目標庫,developer_month_orders_data為表名。意思就是從查詢庫a中建立同名FOREIGN關聯(lián)表,
可以查詢目標庫中的數(shù)據(jù)。以下命令在需要建立的關聯(lián)庫中執(zhí)行。
目標庫中的表必須存在,也就是先建立好,否則從a庫,查詢會報找不到表錯誤
阿里云RDS,數(shù)據(jù)庫:PostgreSQL 9.4,跨實例數(shù)據(jù)庫不支持postgres_fdw建立外部表,坑??!
阿里云技術回復RDS需要10.0版本的 postgresql才支持跨實例。不然只能同一個實例下的不同數(shù)據(jù)庫之間的外部表。
-- 安裝 postgres_fdw 插件 CREATE EXTENSION postgres_fdw; -- 創(chuàng)建遠程服務 CREATE SERVER remote_server FOREIGN DATA WRAPPER postgres_fdw; -- 查看遠程服務 select * from pg_foreign_server ; -- 修改遠程服務 alter server remote_server options (add hostaddr '11.216.10.215', add port '5432', add dbname 'db_des'); -- SERVER賦權 grant usage on foreign server remote_server to postgres; -- 在本地數(shù)據(jù)庫中創(chuàng)建user mapping CREATE USER MAPPING FOR postgres server remote_server options (user 'postgres', password 'xxxxx'); -- 同樣創(chuàng)建枚舉 CREATE TYPE db_enum AS ENUM ('postgres', 'sqlserver', 'mysql'); -- 查看枚舉類型的oid select oid from pg_type where typname='db_enum'; -- 創(chuàng)建外部表developer_month_orders_data CREATE FOREIGN TABLE developer_month_orders_data( id integer not null, developer_user_id integer, package_id integer, order_month date, create_datetime timestamp ) SERVER remote_server options (schema_name 'public',table_name 'developer_month_orders_data'); -- 查看外部表 select * from developer_month_orders_data;
ddl維護操作:
drop user mapping for postgres server server_remote ; drop server server_remote; drop extension postgres_fdw ; drop foreign table test1;
補充:postgresql postgres_fdw 跨庫查詢
1 安裝擴展
create extension postgres_fdw;
2 本地創(chuàng)建server并查看 該server作用是在本地配置一個連接遠程的信息,下面的配置是要連接到遠程DB名稱是postgres數(shù)據(jù)庫
create server server_remote_rudy_01 foreign data wrapper postgres_fdw options(host ‘192.168.11.44',port ‘5432',dbname ‘vsphere_info');
查詢:
select * from pg_foreign_server ;**
3 創(chuàng)建用戶匹配信息并查看,在本地
for后面的postgres是本地登錄執(zhí)行的用戶名,option里存儲的是遠程的用戶密碼
create user mapping for postgres server server_remote_rudy_01 options(user ‘vsphere',password ‘viadmin');
4 本地創(chuàng)建外部表,指定server
CREATE FOREIGN TABLE v1_cost(sample_time TIMESTAMP,datacenter_id int4,host_id int4 ,cost NUMERIC) server server_remote_rudy_01 options (schema_name ‘public',table_name ‘vi_cost');
5 –導入指定的表,也可以不導入指定的表,也可以導入整個schema下面的表(可有可無的一步)
IMPORT FOREIGN SCHEMA public FROM SERVER server_remote_rudy_01 INTO public; IMPORT FOREIGN SCHEMA public limit to(t1) FROM SERVER server_remote_rudy_01 INTO public;
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
PostgreSQL存儲過程循環(huán)調(diào)用方式
這篇文章主要介紹了PostgreSQL存儲過程循環(huán)調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01postgresql使用filter進行多維度聚合的解決方法
這篇文章給大家介紹postgresql使用filter進行多維度聚合的解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-07-07postgresql關于like%xxx%的優(yōu)化操作
這篇文章主要介紹了postgresql關于like%xxx%的優(yōu)化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL進行數(shù)據(jù)導入和導出的操作代碼
在數(shù)據(jù)庫管理中,數(shù)據(jù)的導入和導出是非常常見的操作,特別是在 PostgreSQL 中,提供了多種工具和方法來實現(xiàn)數(shù)據(jù)的有效管理,本文將詳細介紹在 PostgreSQL 中如何進行數(shù)據(jù)導入和導出,并給出具體的命令及示例,需要的朋友可以參考下2024-10-10對PostgreSQL中的慢查詢進行分析和優(yōu)化的操作指南
在數(shù)據(jù)庫的世界里,慢查詢就像是路上的絆腳石,讓數(shù)據(jù)處理的道路變得崎嶇不平,想象一下,你正在高速公路上飛馳,突然遇到一堆減速帶,那感覺肯定糟透了,本文介紹了怎樣對?PostgreSQL?中的慢查詢進行分析和優(yōu)化,需要的朋友可以參考下2024-07-07PostgreSQL 數(shù)據(jù)庫性能提升的幾個方面
PostgreSQL提供了一些幫助提升性能的功能。主要有一些幾個方面。2009-09-09淺談postgresql數(shù)據(jù)庫varchar、char、text的比較
這篇文章主要介紹了淺談postgresql數(shù)據(jù)庫varchar、char、text的比較,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12