PostgreSQL的postgres_fdw跨庫使用

PostgreSQL的數據庫邏輯上是相互獨立的,和Oracle相似,若是要訪問其餘數據庫,須要作跨庫操做,Postgres自己提供了一些擴展,好比dblink,pgsql_fdw等,高版本的建議使用postgres_fdw,也就是pgsql_fdw的升級版。

1、環境介紹
fdw是foreign-data wrapper的一個簡稱,能夠叫外部封裝數據,以前介紹過file_fdw,dblink http://my.oschina.net/Kenyon/blog/55294
http://my.oschina.net/Kenyon/blog/165432
而postgres_fdw實現的是各個postgresql數據庫及遠程數據庫之間的跨庫操做,功能和dblink同樣。

本地 10.1.11.72 DB_port 5432
遠程 10.1.11.71 DB_port 5432

71遠端數據準備
postgres=# show search_path;
 search_path 
-------------
 schema_fdw
(1 row)
postgres=# create table tbl_kenyon (id int,remark text);
CREATE TABLE
postgres=# insert into tbl_kenyon select generate_series(1,100),'Kenyon Go!';
INSERT 0 100
2、安裝使用
 安裝分4步走
1.本地安裝extension
安裝的擴展名是來自於share/extension/*.control中的文件名*,好比postgres_fdw.control
postgres=# create extension postgres_fdw;
CREATE EXTENSION
postgres=# select * from pg_extension ;
 extname     | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition   -------------------+----------+--------------+----------------+------------+-----------+
 plpgsql            |       10 |           11 | f              | 1.0        |           | 
 pg_stat_statements |       10 |         2200 | t              | 1.1        |           | 
 postgres_fdw       |       10 |         2200 | t              | 1.0        |           | 
(3 rows)
postgres=# select * from pg_foreign_data_wrapper;
  fdwname    | fdwowner | fdwhandler | fdwvalidator | fdwacl | fdwoptions 
  ------------+----------+------------+--------------+--------+------------
 postgres_fdw |       10 |     154356 |       154357 |        | 
(1 row)

或
postgres=# \dx
                                     List of installed extensions
      Name        | Version |  Schema |                        Description                     -----------------+---------+------------+-----------------------------------------------------
 pg_stat_statements | 1.1     | public |track execution statistics of all SQL statements executed
 plpgsql            | 1.0     | pg_catalog | PL/pgSQL procedural language
 postgres_fdw       | 1.0     | public     | foreign-data wrapper for remote PostgreSQL servers
(3 rows)
2.本地建立server並查看
該server做用是在本地配置一個鏈接遠程的信息,下面的配置是要鏈接到遠程DB名稱是postgres數據庫
postgres=# create server server_remote_71 foreign data wrapper postgres_fdw options(host '10.1.11.71',port '5432',dbname 'postgres');
CREATE SERVER
postgres=# select * from pg_foreign_server ;
     srvname      | srvowner | srvfdw | srvtype | srvversion | srvacl |   srvoptions  
--------------+----------+--------+---------+------------+--------+--------------------------
server_remote_71 |       10 | 154358 |         |     |{host=10.1.11.71,port=5432,dbname=postgres}
(1 row)
或者
postgres=# \des
              List of foreign servers
       Name       |  Owner   | Foreign-data wrapper 
------------------+----------+----------------------
 server_remote_71 | postgres | postgres_fdw
(1 row)
3.建立用戶匹配信息並查看
--for後面的postgres是本地登陸執行的用戶名,option裏存儲的是遠程的用戶密碼
postgres=# create user mapping for usr_pg_fdw server server_remote_71 options(user 'usr_pg_fdw',password '123456');
CREATE USER MAPPING
postgres=# select * from pg_user_mappings;
  umid  | srvid  |     srvname      | umuser | usename  |             umoptions             
--------+--------+------------------+--------+----------+-----------------------------------
 154360 | 154359 | server_remote_71 |     10 | postgres | {user=usr_pg_fdw,password=123456}
(1 row)
或
postgres=# \deu+
                          List of user mappings
      Server      | User name |               FDW Options                
------------------+-----------+------------------------------------------
 server_remote_71 | postgres  | ("user" 'usr_pg_fdw', password '123456')
(1 row)
4.本地建立外部表,指定server
postgres=# CREATE FOREIGN TABLE tbl_kenyon(id int,remark text) server server_remote_71 options (schema_name 'schema_fdw',table_name 'tbl_test');
CREATE FOREIGN TABLE

--若是不指定options,數據庫會自動匹配相同的表名和表結構,若是有一項不匹配就會報錯表或字段不對應的錯誤
--options能夠指定對應的schema和表名等
5.配置pg_hba.conf
此處主要是在遠端配置本地能訪問的策略,略

6.本地訪問遠端,支持遠程select和DML,和本地表操做同樣
postgres=# select * from tbl_kenyon limit 10;
 id |   remark   
----+------------
  1 | Kenyon Go!
  2 | Kenyon Go!
  3 | Kenyon Go!
  4 | Kenyon Go!
  5 | Kenyon Go!
  6 | Kenyon Go!
  7 | Kenyon Go!
  8 | Kenyon Go!
  9 | Kenyon Go!
 10 | Kenyon Go!
(10 rows)

--本地更新遠程數據
postgres=# delete from tbl_kenyon where id < 10;
DELETE 9
postgres=# select * from tbl_kenyon limit 10;
 id |   remark   
----+------------
 10 | Kenyon Go!
 11 | Kenyon Go!
 12 | Kenyon Go!
 13 | Kenyon Go!
 14 | Kenyon Go!
 15 | Kenyon Go!
 16 | Kenyon Go!
 17 | Kenyon Go!
 18 | Kenyon Go!
 19 | Kenyon Go!
(10 rows)
3、相關係統表
select * from pg_extension;
select * from pg_foreign_data_wrapper;
select * from pg_foreign_server;
select * from pg_foreign_table;
4、清理擴展
postgres=# drop foreign table tbl_kenyon;
DROP FOREIGN TABLE
postgres=# drop user mapping for postgres server server_remote_71 ;
DROP USER MAPPING
postgres=# drop server server_remote_71 ;
DROP SERVER
postgres=# drop extension postgres_fdw ;
DROP EXTENSION
5、相關問題
1.ERROR:  user mapping not found for "postgres"
檢查一下user mapping用戶信息,執行用戶須要與user mapping的第一個用戶相匹配

2.pg_fdw=> select * from tbl_kenyon limit 2;
ERROR:  could not connect to server "pg_remote_71"
DETAIL:  FATAL:  password authentication failed for user "usr_pg_fdw" 
檢查一下options裏面的用戶密碼與遠程用戶密碼是否匹配
6、總結 1.postgres_fdw和dblink實現的功能是同樣的,可是配置使用postgres_fdw更簡單,並且也支持遠程更新,穩定性和方便性考慮更推薦postgres_fdw,較像一個能夠更新遠程數據庫的視圖 2.在數據遷移或者ETL及定時刷新上面會比較有用處 3.外部表實際不佔存儲空間 4.物理表和外部表不能同名,由於pg_class的對象名稱惟一鍵的緣故 5.遠程改掉用戶密碼對當前本地鏈接無效,但本地再次鏈接取數會報錯
相關文章
相關標籤/搜索