PostgesSQL外部數據封裝FDW
- 1. FDW外部數據配置(單表)
- 1.1 遠端數據庫創建測試表
- 1.2 安裝擴展postges\_fdw
- 1.3 創建外部服務SERVER
- 1.4 創建用戶映射USER MAPPING
- 1.5 創建遠程表FOREIGN TABLE
- 1.6 數據庫更新測試
- 2. FDW外部數據配置(用戶)
- 2.1 遠端數據庫創建測試表
- 2.2 創建遠程schema
- 2.3 導入遠程schema
- 2.4 查看遠程schema表
- 3. FDW運維操作
- 3.1 外部表設置只讀操作
- 3.2 刪除增加外部表列
- 3.3 模式導入部分表
POSTGRESQL FDW 的名詞是 foreign-data wrapper 外部數據包裝, 此模塊提供的功能與較舊的 dblink 模塊的功能有很大重疊。但是
postgres_fdw
提供了更透明且符合標準的語法來訪問遠程表,并且在許多情況下可以提供更好的性能。
要準備使用 postgres_fdw
進行遠程訪問
-
使用 CREATE EXTENSION 安裝
postgres_fdw
擴展。 -
使用 CREATE SERVER 創建外部服務器對象,以表示您要連接的每個遠程數據庫。將連接信息指定為服務器對象的選項,但
user
和password
除外。 -
使用 CREATE USER MAPPING 為您要允許訪問每個外部服務器的每個數據庫用戶創建用戶映射。將要使用的遠程用戶名和密碼指定為用戶映射的
user
和password
選項。 -
使用 CREATE FOREIGN TABLE 或 IMPORT FOREIGN SCHEMA 為您要訪問的每個遠程表創建外部表。外部表的列必須與引用的遠程表匹配。但是,如果將正確的遠程名稱指定為外部表對象的選項,則可以使用與遠程表不同的表和/或列名。
1. FDW外部數據配置(單表)
1.1 遠端數據庫創建測試表
postgres=# create database testdb13;
CREATE DATABASE
postgres=# \lList of databasesName | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| | | | | postgres=CTc/postgrestemplate1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| | | | | postgres=CTc/postgrestestdb13 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
postgres=# create user u13 with password 'u13';
CREATE ROLE
postgres=# \c testdb13 u13
Password for user u13:
You are now connected to database "testdb13" as user "u13".
testdb13=>
testdb13=> create table t1 (id int,name varchar);
CREATE TABLE
testdb13=> insert into t1 values(1,'A');
INSERT 0 1
testdb13=> select * from t1;id | name
----+------1 | A
(1 row)
testdb13=> \dt+List of relationsSchema | Name | Type | Owner | Persistence | Size | Description
--------+------+-------+-------+-------------+-------+-------------public | t1 | table | u13 | permanent | 16 kB |
1.2 安裝擴展postges_fdw
已安裝擴展
postgres=# \dxList of installed extensionsName | Version | Schema | Description
---------+---------+------------+------------------------------plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(1 row)
安裝報錯,需先在本地軟件安裝
postgres=# create extension postges_fdw;
ERROR: extension "postges_fdw" is not available
DETAIL: Could not open extension control file "/pgsql/app/pg16/share/extension/postges_fdw.control": No such file or directory.
HINT: The extension must first be installed on the system where PostgreSQL is running.
進入到編譯安裝目錄,編輯安裝postgres_fdw
[root@pgdb ~]# cd /pgsql/soft/postgresql-16.1/contrib/postgres_fdw
[root@pgdb postgres_fdw]# ls
connection.c deparse.c expected meson.build option.o postgres_fdw--1.0.sql postgres_fdw.control postgres_fdw.o shippable.c sql
connection.o deparse.o Makefile option.c postgres_fdw--1.0--1.1.sql postgres_fdw.c postgres_fdw.h postgres_fdw.so shippable.o
[root@pgdb postgres_fdw]# make
[root@pgdb postgres_fdw]# make install
安裝擴展postgres_fdw
postgres=# create extension postgres_fdw ;
CREATE EXTENSION
postgres=# \dxList of installed extensionsName | Version | Schema | Description
--------------+---------+------------+----------------------------------------------------plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural languagepostgres_fdw | 1.1 | public | foreign-data wrapper for remote PostgreSQL servers
(2 rows)
1.3 創建外部服務SERVER
創建一個服務器名pg_fdw_server 使用的類型為postgres_fdw 連接的數據庫為 dbname 為 testdb13
postgres=# CREATE SERVER pg_fdw_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '192.168.1.100', port '5432', dbname 'testdb13');
CREATE SERVER
查看創建server
postgres=# \desList of foreign serversName | Owner | Foreign-data wrapper
---------------+----------+----------------------pg_fdw_server | postgres | postgres_fdw
(1 row)
postgres=# \des+List of foreign serversName | Owner | Foreign-data wrapper | Access privileges | Type | Version | FDW options | Description
---------------+----------+----------------------+-------------------+------+---------+--------------------------------------------------------+-------------pg_fdw_server | postgres | postgres_fdw | | | | (host '192.168.1.100', port '5432', dbname 'testdb13') |
(1 row)
1.4 創建用戶映射USER MAPPING
postgres=# CREATE USER MAPPING FOR postgres SERVER pg_fdw_server OPTIONS (user 'u13', password 'u13');
CREATE USER MAPPING
postgres=# \deu+List of user mappingsServer | User name | FDW options
---------------+-----------+--------------------------------pg_fdw_server | u109 | ("user" 'u13', password 'u13')
(1 row)
1.5 創建遠程表FOREIGN TABLE
postgres=# CREATE FOREIGN TABLE fdw_t1(id int,name varchar) SERVER pg_fdw_server OPTIONS (schema_name 'public',table_name 't1');
CREATE FOREIGN TABLE
postgres=# \det+List of foreign tablesSchema | Table | Server | FDW options | Description
--------+--------+---------------+-----------------------------------------+-------------public | fdw_t1 | pg_fdw_server | (schema_name 'public', table_name 't1') | postgres=# select * from fdw_t1;id | name
----+------1 | A
(1 row)
1.6 數據庫更新測試
遠端數據庫insert數據
postgres=# \c testdb13 u13
Password for user u13:
You are now connected to database "testdb13" as user "u13".
testdb13=> insert into t1 values(2,'B');
INSERT 0 1
本地數據庫訪問遠程表,實時同步過來。
postgres=# select * from fdw_t1;id | name
----+------1 | A2 | B
(2 rows)
2. FDW外部數據配置(用戶)
2.1 遠端數據庫創建測試表
testdb13=> create table t2 (id int,name varchar);
CREATE TABLE
testdb13=> create table t3 (id int,name varchar);
CREATE TABLE
testdb13=> \dt+List of relationsSchema | Name | Type | Owner | Persistence | Size | Description
--------+------+-------+-------+-------------+------------+-------------public | t1 | table | u13 | permanent | 16 kB | public | t2 | table | u13 | permanent | 8192 bytes | public | t3 | table | u13 | permanent | 8192 bytes |
(3 rows)
2.2 創建遠程schema
postgres=# create schema remote_fdw_schema;
CREATE SCHEMA
2.3 導入遠程schema
postgres=# import foreign schema public from server pg_fdw_server into remote_fdw_schema options ( import_default 'true');
IMPORT FOREIGN SCHEMA
2.4 查看遠程schema表
postgres=# \det+ remote_fdw_schema.*List of foreign tablesSchema | Table | Server | FDW options | Description
-------------------+-------+---------------+-----------------------------------------+-------------remote_fdw_schema | t1 | pg_fdw_server | (schema_name 'public', table_name 't1') | remote_fdw_schema | t2 | pg_fdw_server | (schema_name 'public', table_name 't2') | remote_fdw_schema | t3 | pg_fdw_server | (schema_name 'public', table_name 't3') |
(3 rows)
postgres=# select * from remote_fdw_schema.t2;id | name
----+------
(0 rows)postgres=# select * from remote_fdw_schema.t1;id | name
----+------1 | A2 | B
(2 rows)postgres=# select * from remote_fdw_schema.t3;id | name
----+------
(0 rows)
遠端數據庫更新數據測試
postgres=# \c testdb13 u13
testdb13=> insert into t2 values(22,'BB');
INSERT 0 1
testdb13=> insert into t3 values(33,'CC');
INSERT 0 1
本地數據庫查看遠程schema表
postgres=# select * from remote_fdw_schema.t2;id | name
----+------22 | BB
(1 row)
postgres=# select * from remote_fdw_schema.t3;id | name
----+------33 | CC
(1 row)
3. FDW運維操作
3.1 外部表設置只讀操作
postgres=# insert into remote_fdw_schema.t1 values (3,'C');
INSERT 0 1
postgres=# select * from remote_fdw_schema.t1;id | name
----+------1 | A2 | B3 | C
(3 rows)
修改為只讀操作,false改為true讀寫模式
postgres=# alter foreign table remote_fdw_schema.t1 options (add updatable 'false');
ALTER FOREIGN TABLE
postgres=# insert into remote_fdw_schema.t1 values (4,'D');
ERROR: foreign table "t1" does not allow inserts
3.2 刪除增加外部表列
刪除列
postgres=# select * from remote_fdw_schema.t1;id | name
----+------1 | A2 | B3 | C
(3 rows)
postgres=# alter foreign table remote_fdw_schema.t1 drop column name;
ALTER FOREIGN TABLE
postgres=# select * from remote_fdw_schema.t1;id
----123
(3 rows)
增加列
postgres=# alter foreign table remote_fdw_schema.t1 add column name varchar;
ALTER FOREIGN TABLE
postgres=# select * from remote_fdw_schema.t1;id | name
----+------1 | A2 | B3 | C
(3 rows)
3.3 模式導入部分表
- Limit to 指定哪些表導入
postgres=# \det_ remote_fdw_schema.*List of foreign tablesSchema | Table | Server
--------+-------+--------
(0 rows)
postgres=# import foreign schema public limit to ( t1,t2) from server pg_fdw_server into remote_fdw_schema options ( import_default 'true');
IMPORT FOREIGN SCHEMA
postgres=# \det_ remote_fdw_schema.*List of foreign tablesSchema | Table | Server
-------------------+-------+---------------remote_fdw_schema | t1 | pg_fdw_serverremote_fdw_schema | t2 | pg_fdw_server
(2 rows)
- Except 指定哪些表不要導入
postgres=# \det_ remote_fdw_schema.*List of foreign tablesSchema | Table | Server
--------+-------+--------
(0 rows)
postgres=# import foreign schema public except ( t1,t3) from server pg_fdw_server into remote_fdw_schema options ( import_default 'true');
IMPORT FOREIGN SCHEMA
postgres=# \det_ remote_fdw_schema.*List of foreign tablesSchema | Table | Server
-------------------+-------+---------------remote_fdw_schema | t2 | pg_fdw_server
(1 row)