psql 命令行字符串中的换行符何时重要?

when are newlines in psql command line strings significant?

我试图打破涉及 psql 命令字符串(即 psql -c)的长命令行,这似乎会导致错误。例如,对于 PostgreSQL 9.5 和 Ubuntu 16.04:

$ psql -c "\dt"

工作正常,而

$ psql -c "
> \dt
> "

生成:

ERROR:  syntax error at or near "\"
LINE 2: \dt

出于好奇,什么时候可以在 psql 命令字符串中插入换行符(即 \n)?

command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single backslash command.

https://www.postgresql.org/docs/current/static/app-psql.html

psql 似乎不理解带有前导新行的反斜杠命令。

作为替代方法,您可以使用管道 echo 命令,也在文档中进行了描述。例如:

$ echo '
> \d
> select 1 as x;' | psql postgres

        List of relations
 Schema | Name  | Type |  Owner   
--------+-------+------+----------
 public | dummy | view | postgres
(1 row)

 x 
---
 1
(1 row)