Postgresql - Foreign data wrappers - postgres_fdw

将外部数据库当作外部表访问,可以访问PG,Oracle,MySQL,MS SQL Server,SQLite等。

我们将在以下环境中进行实验:
CentOS 7 + PG 10.4

访问其他PG server实例
1. Extension
mytest=# create extension postgres_fdw;
CREATE EXTENSION

2. Server
mytest=# create server pg_03ac FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '192.168.4.13', dbname 'mytest', port '5432');
CREATE SERVER

3. User Mapping
mytest=# create user MAPPING FOR mytest SERVER pg_03ac options (user 'chen', password '');
CREATE USER MAPPING

4. Create foreign table
mytest=# create foreign table test_03ac(id int, col1 varchar(10), col2 varchar(10), col3 varchar(10), col4 date) server pg_03ac options (schema_name 'public' , table_name 'test');
CREATE FOREIGN TABLE
# Verify
mytest=# select * from test_03ac;
id | col1 | col2 | col3 | col4
----+------+------+------+------------
1 | aa | aa | aa | 2018-05-22
2 | bb | bb | bb | 2018-05-22
3 | cc | cc | ee | 2018-05-22
(3 rows)

6. Import Foreign Schema
mytest=# import foreign schema public limit to (test,test01) from server pg_03ac into public;
IMPORT FOREIGN SCHEMA
# Verify
mytest=# select * from test;
id | col1 | col2 | col3 | col4
----+------+------+------+------------
1 | aa | aa | aa | 2018-05-22
2 | bb | bb | bb | 2018-05-22
3 | cc | cc | ee | 2018-05-22
4 | d | dd | ddd | 2018-07-10
(4 rows)

mytest=# select * from test01;
col1 | col2 | col3
------+------+----------------------------
1 | a | 2018-04-13 00:56:51.157712
2 | b | 2018-07-10 23:27:11.993468
(2 rows)


======================================================================
官方给出的create foreign table 和 import foreign schema的语句:

CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [ { column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ] | table_constraint } [, ... ]] )[ INHERITS ( parent_table [, ... ] ) ] SERVER server_name[ OPTIONS ( option 'value' [, ... ] ) ]CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name PARTITION OF parent_table [ ( { column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ] | table_constraint } [, ... ]) ] partition_bound_spec SERVER server_name[ OPTIONS ( option 'value' [, ... ] ) ]where column_constraint is:[ CONSTRAINT constraint_name ]{ NOT NULL | NULL | CHECK ( expression ) [ NO INHERIT ] | DEFAULT default_expr }and table_constraint is:[ CONSTRAINT constraint_name ]CHECK ( expression ) [ NO INHERIT ]


IMPORT FOREIGN SCHEMA remote_schema [ { LIMIT TO | EXCEPT } ( table_name [, ...] ) ] FROM SERVER server_name INTO local_schema [ OPTIONS ( option 'value' [, ... ] ) ]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值