我正在生成一个 SQL 命令列表来导出一些我最终使用 psql -f 运行的数据。查询都获得相同的数据子集,所以我想我会考虑资格,并将符合条件的用户 ID 列表放入临时表中,如下所示
create temporary table tmp_export_users as (select id from users where ...)
Run Code Online (Sandbox Code Playgroud)
然后在我的 \copy 命令中参考
\copy (select ... from table where user_id in (select id from tmp_export_users)) TO 'filename.csv' WITH CSV HEADER
Run Code Online (Sandbox Code Playgroud)
这些都在同一个文件中,每行一个,并运行它们 -f 我得到复制命令看不到临时表的错误,所以我猜客户端复制命令实际上不能使用相同的 postgres会话为 psql。
那是对的吗?有没有办法改变这种行为?