我可以在PostgreSQL函数中使用\copy命令吗? [英] Can I use \copy command into a function of postgresql?

查看:628
本文介绍了我可以在PostgreSQL函数中使用\copy命令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此语句创建为函数:

I am trying to create this statement into a function:

\copy aux("nombre") TO '/home/david/lugares.csv' delimiters ';';

所以我下一个:

 CREATE OR REPLACE FUNCTION crearcsv()
      RETURNS void AS
    $BODY$
    DECLARE STATEMENT TEXT; 
    BEGIN
        RAISE NOTICE 'CREAR CSV';
        STATEMENT:= '\copy aux ("nombre") TO ''/home/david/lugares.csv'' delimiters '';'';';    
        RAISE NOTICE '%',STATEMENT;
        EXECUTE STATEMENT;
    END;$BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;

但是当我调用该函数时,我得到了下一个:

But I get the next when I call to the function:


注意:\将aux( nombre)复制到'/home/david/lugares.csv'分隔符';';
错误: \或附近的语法错误
第1行:\将aux( nombre)复制到'/home/david/lugares.csv'分隔符...
^

查询:\copy aux( nombre)TO'/home/david/lugares.csv'delimiters';';

语境:PL / pgSQL在执行语句中的函数crearcsv()第7行**

NOTICE: \copy aux ("nombre") TO '/home/david/lugares.csv' delimiters ';'; ERROR: syntax error at or near "\"
LINE 1: \copy aux ("nombre") TO '/home/david/lugares.csv' delimiters... ^
QUERY: \copy aux ("nombre") TO '/home/david/lugares.csv' delimiters ';';
CONTEXT: PL/pgSQL function crearcsv() line 7 at EXECUTE statement**

此语句在PSQL控制台中正常工作

This statement works fine in PSQL console

有帮助吗?

推荐答案

您只需更改 \copy 副本中。 COPY \copy 的 sql变体,在数据库函数中工作,语法相同,但具有可能与您相关的一些区别:

You can simply change \copy in copy. COPY is the "sql variant" of \copy, works in a database function, the syntax is identical but has some differences which can be relevant for you:


COPY是Postgres数据加载方法。 Postgres的COPY有
两种不同的变体,COPY和\COPY:COPY基于服务器,\COPY基于
客户。

COPY is the Postgres method of data-loading. Postgres's COPY comes in two separate variants, COPY and \COPY: COPY is server based, \COPY is client based.

COPY将由PostgreSQL后端(用户 postgres)运行。
后端用户需要读取&以
的顺序写入数据文件,以便从中复制数据。您需要使用
COPY的绝对路径名。 \COPY另一方面,在当前$ USER下运行,并在
用户环境下运行。 \COPY可以处理相对路径名。因此,如果
psql \COPY处理您的
所需要的内容,它会更容易使用。

COPY will be run by the PostgreSQL backend (user "postgres"). The backend user requires permissions to read & write to the data file in order to copy from/to it. You need to use an absolute pathname with COPY. \COPY on the other hand, runs under the current $USER, and with that users environment. And \COPY can handle relative pathnames. The psql \COPY is accordingly much easier to use if it handles what you need.

使用这两种方法,您还都需要可以在表上进行插入/更新或选择
权限,以便在表上进行复制或复制。

With either of these you'll also need to have insert/update or select permission on the table in order to COPY to or from it.

来自 https://wiki.postgresql.org/wiki/COPY

主要区别是 COPY 会将输出文件写在运行postgres服务器的文件系统上,而不是在执行<$的服务器上c $ c> COPY 。如果您有在本地主机上运行的Postgres服务器,也是如此,但是在更复杂的情况下可能会成为一个大问题。

The main difference is that COPY will write the output file on the file system where the postgres server is running, not on the server where you execute COPY. This will be the same, if you have a postgres server running on localhost, but can be big problem by more complex scenarios.

另请参见文档: http://www.postgresql.org/docs/9.3/static/sql-copy。 html

和以下答案:将PostgreSQL的PL / pgSQL输出保存到CSV文件中

这篇关于我可以在PostgreSQL函数中使用\copy命令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆