天天看點

postgresql 外部表簡單測試

postgresql 外部表測試

系統 debian-6.0.4

資料庫 postgresql-9.3.0

#########################################################################################

建立擴充,因為預設不安裝,安裝需要手工建立

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

----------+---------+------------+--------------------------------------------------------------

dblink | 1.1 | public | connect to other PostgreSQL databases from within a database

file_fdw | 1.0 | public | foreign-data wrapper for flat file access

plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language

(3 rows)

建立外部表服務接口,檢視

create foreign table tab_x1(ISBN varchar(80), bookname varchar(1000),printing varchar(80),publicationdate varchar(80), price numeric(15,5), discount numeric(15,5), stock int, location varchar(80), classes varchar(30), remarks varchar(30)) server pg_file_server options(filename '/var/lib/postgresql/9.3/x1.csv',format 'csv',header 'true',delimiter ',',null '1');

--後面options裡面參數的說明

--filename後面是檔案名和絕對路徑

--format是格式,csv是逗号分隔,text表示是tab分隔的方式

--delimiter是分隔符

--header表示第一行資料是否需要

--null表示空資料的轉化處理,例子中字段1将轉化為null

上傳外部檔案

上傳外部檔案到相應目錄如本例中的/var/lib/postgresql/9.3/x1.csv

查詢

postgres=# select * from tab_x1 limit 10;

isbn | bookname | printing | publicationdate | price | discount | stock | location | classes | remarks

----------------+------------------------+----------+-----------------+----------+----------+-------+----------+---------+---------

9787539138855 | 十一月,楓火燎原 | 21世紀 | 2007年11月 | 9.80000 | 0.13000 | 1 | 1007 | I | 特價

9787533654962 | ×××2 | 安徽教育 | 2010年6月 | 23.00000 | 0.13000 | 43 | 1204 | I | 特價

9787533654979 | 一個中國人在中國的遭遇 | 安徽教育 | 2010年3月 | 25.00000 | 0.13000 | 128 | 3602 | I | 特價

9787533728403 | 水榭 | 安徽科技 | 2004年2月 | 60.00000 | 0.13000 | 2 | 307 | T | 特價

9787533728915 | 建築與裝飾工程造價問答 | 安徽科技 | 2005年1月 | 32.80000 | 0.13000 | 2 | 3101 | T | 特價

9787533732257 | 西藏旅遊攝影指南 | 安徽科技 | 2005年5月 | 8.00000 | 0.13000 | 1 | 2007 | K | 特價

9787533733322 | 異案良方 | 安徽科技 | 2005年9月 | 38.00000 | 0.13000 | 2 | 3005 | R | 特價

9787533733384 | 複式住宅裝潢 | 安徽科技 | 2005年10月 | 32.00000 | 0.13000 | 1 | 2707 | T | 特價

9787563708253 | 旅遊教育出版社 | 安徽科技 | 2002年10月 | 12.90000 | 0.13000 | 2 | 507 | K | 特價

9787539808307 | 環境藝術設計 | 安徽美術 | 2000年9月 | 18.00000 | 0.13000 | 1 | 3007 | T | 特價

(10 rows)

檢視外部表

postgres=# \d+ tab_x1

Foreign table "public.tab_x1"

Column | Type | Modifiers | FDW Options | Storage | Stats target | Description

-----------------+-------------------------+-----------+-------------+----------+--------------+-------------

isbn | character varying(80) | | | extended | |

bookname | character varying(1000) | | | extended | |

printing | character varying(80) | | | extended | |

publicationdate | character varying(80) | | | extended | |

price | numeric(15,5) | | | main | |

discount | numeric(15,5) | | | main | |

stock | integer | | | plain | |

location | character varying(80) | | | extended | |

classes | character varying(30) | | | extended | |

remarks | character varying(30) | | | extended | |

Server: pg_file_server

繼續閱讀