简体   繁体   English

如何将变量传递给 Postgresql 中的复制命令

[英]how to pass variable to copy command in Postgresql

I tried to make a variable in SQL statement in Postgresql, but it did not work.我试图在Postgresql中的SQL语句中创建一个变量,但是没有用。 There are many csv files stored under the path.路径下存放了很多csv个文件。 I want to set path in Postgresql that can tell copy command where can find csv files.我想在 Postgresql 中设置路径,它可以告诉复制命令在哪里可以找到 csv 文件。

SQL statement sample: SQL 报表样本:

\set outpath '/home/clients/ats-dev/'
\COPY licenses (_id, name,number_seats ) FROM :outpath + 'licenses.csv' CSV HEADER DELIMITER ',';
\COPY uploaded_files (_id, added_date ) FROM :outpath + 'files.csv' CSV HEADER DELIMITER ',';

It did not work.那没起效。 I got error: no such files.我得到错误:没有这样的文件。 The two files licneses.csv and files.csv are stored under /home/cilents/ats-dev on Ubuntu. I found some sultion that use "\set file 'license.csv'". licneses.csv 和 files.csv 这两个文件存储在 Ubuntu 上的 /home/cilents/ats-dev 下。我发现了一些使用“\set file 'license.csv'”的命令。 It did not work for me becacuse I have many csv files.它对我不起作用,因为我有很多 csv 文件。 also I tried to use "from: outpath || 'licenses.csv'".我也尝试使用“from: outpath || 'licenses.csv'”。 it did not work ether.它没有工作醚。 Appreciate for any helps.感谢您的帮助。

Using 9.3.使用 9.3。

It looks like psql does not support :variable substitution within psql backslash commands. 看起来psql不支持:variable psql反斜杠命令中的:variable替换。

test=> \set somevar fred
test=> \copy z from :somevar
:somevar: No such file or directory

so you will need to do this via an external tool like the unix shell. 因此,您需要通过外部工具(如unix shell)执行此操作。 eg 例如

for f in *.sql; do
    psql -c "\\copy $(basename $f) FROM '$f'"
done

You can try COPY command 您可以尝试COPY命令

\set outpath '\'/home/clients/ats-dev/'
COPY licenses (_id, name,number_seats ) FROM :outpath/licenses.csv' WITH CSV HEADER DELIMITER ',';
COPY uploaded_files (_id, added_date ) FROM :outpath/files.csv' WITH CSV HEADER DELIMITER ',';

Note: Files named in a COPY command are read or written directly by the server, not by the client application. 注意:COPY命令中指定的文件由服务器直接读取或写入,而不是由客户端应用程序读取或写入。 Therefore, they must reside on or be accessible to the database server machine, not the client. 因此,它们必须驻留在数据库服务器计算机上,或者可以访问它们,而不是客户端。 They must be accessible to and readable or writable by the PostgreSQL user (the user ID the server runs as), not the client. 它们必须是PostgreSQL用户(服务器运行的用户ID)可访问,可读或可写,而不是客户端。 Similarly, the command specified with PROGRAM is executed directly by the server, not by the client application, must be executable by the PostgreSQL user. 类似地,用PROGRAM指定的命令由服务器直接执行,而不是由客户端应用程序执行,必须由PostgreSQL用户执行。 COPY naming a file or command is only allowed to database superusers, since it allows reading or writing any file that the server has privileges to access. COPY命名文件或命令仅允许数据库超级用户,因为它允许读取或写入服务器有权访问的任何文件。

Documentation: Postgresql 9.3 COPY 文档: Postgresql 9.3 COPY

It may have been true when this was originally asked, that psql backslash commands didn't support variable interpolation, but in my PostgreSQL 14 instance that's no longer the case.最初询问时可能确实如此,psql 反斜杠命令不支持变量插值,但在我的 PostgreSQL 14 实例中,情况已不再如此。 However, the psql manpage is clear that \copy specifically does not support variable interpolation.但是,psql 联机帮助页明确指出 \copy 特别不支持变量插值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM