2011-05-24 17 views
50

我想使用psql「\ copy」命令將數據從製表符分隔的文件中提取到Postgres中。我使用這個命令:如何在postgres前端指定一個選項卡COPY

\copy cm_state from 'state.data' with delimiter '\t' null as ; 

但我發現了這樣的警告(表實際加載罰款):

WARNING: nonstandard use of escape in a string literal 
LINE 1: COPY cm_state FROM STDIN DELIMITER '\t' NULL AS ';' 
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'. 

如何指定一個選項卡,如果「\ t」是不正確的?

+10

嘗試什麼錯誤信息提示:'從「state.data」與\副本cm_state分隔符E'\ t'null as';'' – 2011-05-24 15:48:46

+0

是的,你說得對。我沒有看到錯誤信息中的前導「E」。非常感謝! – 2011-05-24 16:02:29

+1

E開始轉義序列。想想E,就像你在C中的雙引號字符串一樣。E'\ t'==「\ t」。 – Sean 2011-05-24 19:59:52

回答

112

使用E'\t'告訴PostgreSQL的有可能被轉義字符有:

\copy cm_state from 'state.data' with delimiter E'\t' null as ; 
+7

謝謝!這也解決了對我來說「COPY分隔符必須是單個單字節字符」的信息量較少的錯誤。在這裏添加它,以便搜索引擎可以接受它。 – 2015-11-26 10:15:53

+0

這應該是另一個錯誤消息的答案。只是爲了讓搜索引擎撿起:) – 2016-04-07 08:25:14

2

你可以做到這一點copy cm_state from stdin with (format 'text')

+0

文本文件的默認分隔符是一個選項卡..我已經使用這個,它的工作.. https://www.postgresql.org/docs/9.2/static/sql- copy.html – user4372693 2017-03-08 20:16:42

+0

當你想把這個命令放在一個shell腳本中時,這會有所幫助 - '$$ \ t $$'似乎並不奏效,奇怪。 – Otheus 2017-08-23 18:31:07

相關問題