将CSV文件复制到具有选定列的SQL表

时间:2019-12-07 05:48:27

标签: sql postgresql

我有这样的CSV

#+BEGIN_SRC shell :results output
cat data/supervisor_salaries.csv
#+END_SRC

#+RESULTS:
town,supervisor,salary
Anytown,Jones,27000
Bumblyburg,Baker,24999
Moetown,Smith,32100
Bigville,Kao,31500
New Brillig,Carroll,72690

试图将其写入表

#+begin_src sql :engine postgresql :dbuser org  :dbpassword 1618 :database analysis
\copy supervisor_salaries (town, supervisor, salary) FROM 'data/supervisor_salaries.csv'
WITH (FORMAT CSV, HEADER);
#+end_src

但报告错误:

psql:/tmp/babel-PSl3Td/sql-in-rlHtPW:1: ERROR:  missing data for column "supervisor"
CONTEXT:  COPY supervisor_salaries, line 1: "town,supervisor,salary"

错误“缺少列'supervisor'的数据,但csv文件中确实有超级用户列,这是什么问题?

1 个答案:

答案 0 :(得分:1)

\copy命令是Postgres终端工具psql的特定功能,它不是SQL语句。所有psql命令(以\开头)都必须放在一行中,因此您已经执行了

\copy supervisor_salaries (town, supervisor, salary) FROM 'data/supervisor_salaries.csv'

由于错误定义了文件格式(默认为text)而引发错误。