将文本内容导出到文本文件,不带\n标记

Ale*_*elo 3 postgresql

当我尝试导出字段的文本内容,并且该内容具有回车符时,该字符的输出类似于 \N 字符串。

例如:

create table foo ( txt text );
insert into foo ( txt ) values ( 'first line
second line
...
and other lines');
copy foo ( txt ) to '/tmp/foo.txt';
Run Code Online (Sandbox Code Playgroud)

我想返回以下(a):

first line
second line
...
and other lines
Run Code Online (Sandbox Code Playgroud)

但是,输出是 (b):

first line\Nsecond line\N...\Nand other lines
Run Code Online (Sandbox Code Playgroud)

有人知道如何获得(a)输出吗?

Dan*_*ité 6

\N是因为一行必须对应一个数据库行。

对于 CSV 格式,此规则是宽松的,其中可以使用多行文本,但引号字符(默认情况下:)"会将文本括起来。

如果您想要多行输出并且周围没有封闭字符,则不应使用COPY but SELECT

假设 unix shell 作为调用者的执行环境,你可以这样做:

psql -A -t -d dbname -c 'select txt from foo' >/tmp/file.txt
Run Code Online (Sandbox Code Playgroud)