如何將postgresql數據庫表內數據導出為excel格式(推薦)
在上篇文章給大傢介紹瞭如何將postgresql數據庫表內數據導出為excel格式(推薦) 感興趣的朋友點擊查看。
本文主要用於介紹如何使用copy或者\copy命令將postgresql數據庫內表的數據導出為excel格式,方便用戶查看編輯。
copy命令同\copy命令語法上相同,區別在於copy必須使用能夠超級用戶使用,copy … to file 中的文件都是數據庫服務器所在的服務器上的文件,而\copy 一般用戶即可執行且\copy 保存或者讀取的文件是在客戶端所在的服務器。本文主要以copy命令作為介紹重點,使用copy命令將表內數據倒為csv格式文件即為excel格式。
1、copy命令語法
COPY { 表名 [ ( 列名稱 [, ...] ) ] | ( 查詢 ) } TO { '文件名' | PROGRAM '命令' | STDOUT } [ [ WITH ] ( 選項 [, ...] ) ] 選項可以是下列內容之一 FORMAT 格式_名稱 FREEZE [ 佈爾 ] DELIMITER '分隔字符' NULL '空字符串' HEADER [ 佈爾 ] QUOTE '引用字符' ESCAPE '轉義字符' FORCE_QUOTE { ( 列名稱 [, ...] ) | * } FORCE_NOT_NULL ( 列名稱 [, ...] ) FORCE_NULL ( 列名稱 [, ...] ) ENCODING 'encoding_name(編碼名)'
2、多場景使用介紹
①查看現有表數據
test=# select * from test; user_id | user_name | age | gender | remark ---------+---------------+-----+--------+---------------------------------------------- 1 | Jackie Chan | 45 | male | "police story","project A","rush hour" 3 | Brigitte Li | 46 | female | 4 | Maggie Cheung | 39 | female | 5 | Jet Li | 41 | male | "Fist of Legend","Once Upon a Time in China" 2 | Gong Li | 38 | female | "Farewell My Concubine","Lifetimes Living" (5 行記錄)
②帶列名導出,默認情況下使用,作為分隔符
test=# copy test to '/tmp/test1.csv' with csv header; COPY 5 test=# \! cat /tmp/test1.csv user_id,user_name,age,gender,remark 1,Jackie Chan,45,male,"""police story"",""project A"",""rush hour""" 3,Brigitte Li,46,female, 4,Maggie Cheung,39,female, 5,Jet Li,41,male,"""Fist of Legend"",""Once Upon a Time in China""" 2,Gong Li,38,female,"""Farewell My Concubine"",""Lifetimes Living"
③帶列名導出,指定使用|作為分隔符
test=# copy test to '/tmp/test1.csv' with csv header DELIMITER '|'; COPY 5 test=# \! cat /tmp/test1.csv user_id|user_name|age|gender|remark 1|Jackie Chan|45|male|"""police story"",""project A"",""rush hour""" 3|Brigitte Li|46|female| 4|Maggie Cheung|39|female| 5|Jet Li|41|male|"""Fist of Legend"",""Once Upon a Time in China""" 2|Gong Li|38|female|"""Farewell My Concubine"",""Lifetimes Living"
④帶列名導出,將空字符替換為指定值導出
test=# copy test to '/tmp/test1.csv' with csv header null 'to be supplemented'; COPY 5 test=# \! cat /tmp/test1.csv user_id,user_name,age,gender,remark 1,Jackie Chan,45,male,"""police story"",""project A"",""rush hour""" 3,Brigitte Li,46,female,to be supplemented 4,Maggie Cheung,39,female,to be supplemented 5,Jet Li,41,male,"""Fist of Legend"",""Once Upon a Time in China""" 2,Gong Li,38,female,"""Farewell My Concubine"",""Lifetimes Living"
到此這篇關於如何將postgresql數據庫表內數據導出為excel格式的文章就介紹到這瞭,更多相關postgresq表內數據導出excel格式內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 如何將excel表格數據導入postgresql數據庫
- python基礎篇之pandas常用基本函數匯總
- pandas中df.rename()的具體使用
- Python 操作 MongoDB 講解詳細
- 基於Python實現簡單的定時器詳解