在 postgres psql 命令中使用文件包含设置 search_path

Set search_path with file include in postgres psql command

如何在一个 psql 命令中包含多个搜索路径,以便多个文件可以 运行 具有不同的 search_paths 但在一个事务中都是 运行?

psql
  --single-transaction
  --command="set search_path = 'a'; \i /sqlfile/a.sql; set search_path = 'b'; \i /sqlfile/b.sql;"

当我 运行 执行此操作时,我在 \i 处收到语法错误。我需要单独包含文件,并且它们是动态生成的,所以我宁愿使用 --command 运行 而不是必须生成文件并在可能的情况下使用 --file

The manual about the --command option:

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. Thus you cannot mix SQL and psql meta-commands within a -c option. To achieve that, you could use repeated -c options or pipe the string into psql [...]

大胆强调我的。

尝试:

psql --single-transaction -c 'set search_path = a' -c '\i /sqlfile/a.sql' -c 'set search_path = b' -c '\i /sqlfile/b.sql'

或使用此处文档:

psql --single-transaction <<EOF
set search_path = a;
\i /sqlfile/a.sql
set search_path = b;
\i /sqlfile/b.sql
EOF

search_path 不需要引号,顺便说一句。