參考原文:http://www.open-open.com/lib/view/open1380594167635.htmlhtml
postgresql從9.1開始增長了外部表訪問的功能,這個功能就是數據庫直接讀取數據庫之外的文件,好比csv或者text等類型的文件,暫時不支 持DML。postgresql有各類插件能直連各類異構DB,如oracle_fdw,mysql_fdw,file_fdw等,對數據的遷移是很方便 的,這是PG的擴展性較強的一個表現。這裏介紹foreign data wrapper的file_fdw使用。
1、環境:
OS :CentOS 6.3
DB :PostgreSQL 9.3
2、使用過程
1.建立擴展,由於默認不安裝,安裝須要手工建立mysql
[postgres@kenyon ~]$ psql psql (9.3.0) Type "help" for help. postgres=# CREATE EXTENSION file_fdw; CREATE EXTENSION postgres=# \dx List of installed extensions Name | Version | Schema | Description ----------+---------+------------+------------------------------------------- file_fdw | 1.0 | public | foreign-data wrapper for flat file access plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language (2 rows)
2.建立外部表服務接口,查看sql
postgres=# create server pg_file_server foreign data wrapper file_fdw; CREATE SERVER postgres=# \des List of foreign servers Name | Owner | Foreign-data wrapper ----------------+----------+---------------------- pg_file_server | postgres | file_fdw (1 row)
3.創建一個外部表,與外部文件結構一致數據庫
postgres=# create foreign table tab_area(id int,cname varchar(80),ename varchar(80),create_time varchar(30)) server pg_file_server options(filename '/home/postgres/data1.csv',format 'csv',header on,delimiter ',',null '1'); --後面options裏面參數的說明 --filename後面是文件名和絕對路徑 --format是格式,csv是逗號分隔,text表示是tab分隔的方式 --delimiter是分隔符 --header表示第一行數據是否須要 --null表示空數據的轉化處理,例子中字段1將轉化爲null
4.上傳外部文件oracle
[postgres@kenyon ~]$ more data1.csv ?ID,cname,ename,create_time 1,浙江,zhejiang,2013-01-01 2,杭州,hangzhou,2013-01-02 3,北京,beijing,2012-09-12 4,默認,,2013-09-10 5,四川,sichuan,2012-12-12 6,,nanjing, 7,甘肅,,1998-12-12
5.查詢app
postgres=# select * from tab_area; id | cname | ename | create_time ----+-------+----------+------------- | 浙江 | zhejiang | 2013-01-01 2 | 杭州 | hangzhou | 2013-01-02 3 | 北京 | beijing | 2012-09-12 4 | 默認 | | 2013-09-10 5 | 四川 | sichuan | 2012-12-12 6 | | nanjing | 7 | 甘肅 | | 1998-12-12 (7 rows)
6.查看外部表工具
postgres=# \d+ tab_area Foreign table "public.tab_area" Column | Type | Modifiers | FDW Options | Storage | Stats target | Description -------------+-----------------------+-----------+-------------+----------+--------------+------------- id | integer | | | plain | | cname | character varying(80) | | | extended | | ename | character varying(80) | | | extended | | create_time | character varying(10) | | | extended | | Server: pg_file_server FDW Options: (filename '/home/postgres/data1.csv', format 'csv', delimiter ',', header 'true', "null" '1') Has OIDs: no
7.與實際普通表的結合post
postgres=# create table tab_act_area as select * from tab_area; SELECT 7 postgres=# insert into tab_act_area select * from tab_area where id>2; INSERT 0 5 postgres=# \d+ List of relations Schema | Name | Type | Owner | Size | Description --------+--------------+---------------+----------+------------+------------- public | empsalary | table | postgres | 16 kB | public | tab_act_area | table | postgres | 8192 bytes | public | tab_area | foreign table | postgres | 0 bytes | public | test | table | postgres | 5096 kB | (4 rows)
8.修改外部表spa
postgres=# alter foreign table tab_area options(set null '^null*'); ALTER FOREIGN TABLE postgres=# select * from tab_area; id | cname | ename | create_time ----+-------+----------+------------- 1 | 浙江 | zhejiang | 2013-01-01 2 | 杭州 | hangzhou | 2013-01-02 3 | 北京 | beijing | 2012-09-12 4 | 默認 | | 2013-09-10 5 | 四川 | sichuan | 2012-12-12 6 | | nanjing | 7 | 甘肅 | | 1998-12-12 (7 rows)
其餘語法和普通表相似,除了option的三個選項(drop、add和set) ,具體參考:
.net
http://www.postgresql.org/docs/9.3/static/sql-alterforeigntable.html
三總結:
1.外部表實際不存儲數據,只是一張表結構;
2.9.2版本比9.1版本多了option的選擇,9.3又多了collate一些排序的補充,功能進一步完善,如今已有約束、default普通表的應有功能;
3.實用性,對按期的數據抽取但無需入庫檢查是很方便的,既減小了數據存儲,像基於postgresql的greenplum的數據導入導出就很是實用