为什么不能在多行执行 PSQL 元命令

Why PSQL meta command cannot be execute at mutliline

刚刚发现\COPY命令只能在一行执行,多次尝试答案的查询。 这种方式对于编写长查询似乎很不友好。而且不容易找出原因。 我不认为这是一个错误,那为什么。

从这里 psql 如果您要复制到文件:

Another way to obtain the same result as \copy ... to is to use the SQL COPY ... TO STDOUT command and terminate it with \g filename or \g |program. Unlike \copy, this method allows the command to span multiple lines; also, variable interpolation and backquote expansion can be used.

从文件中复制,借用pg_dump所做的:

CREATE TABLE csv_test (col1 integer, col2 integer);

cat csv_test.csv 

COPY 
  csv_test 
FROM 
 stdin WITH CSV;
"19","9"
"19","5"
"19","5"
"19","15"
"19","5"

\i ~/csv_test.csv 
COPY 5

select * from csv_test ;
 col1 | col2 
------+------
   19 |    9
   19 |    5
   19 |    5
   19 |   15
   19 |    5