【问题标题】:Postgres literal carriage return error on LinuxLinux上的Postgres文字回车错误
【发布时间】:2018-05-02 23:33:37
【问题描述】:

规格
Linux Mint 18.x
Posgres 10.x
pgadmin3

我尝试将 TSV 加载到 postgres 中,但不断收到文字回车错误。在文件中,“\N”用于表示NULL。

我已经尝试过\copy 和 pgadmin3 中的导入对话框。在 pgadmin3 中,我尝试省略文件格式,并尝试将其设置为 UTF8 。错误仍然存​​在。

使用的初始命令:

\copy table FROM PROGRAM 'tail -n +2 /home/super/Downloads/folder/myfile.tsv'

ERROR; literal carriage return found in data
HINT: use "\r" to represent carriage return.

我一直在使用 sed 创建不同版本的文件来替换我认为可能导致错误的文件:

sed 's/\n/\r/g myfile.tsv > newfile1.tsv'
sed 's/\\n/\r/g myfile.tsv > newfile2.tsv'
sed 's/\\n//g myfile.tsv > newfile3.tsv'

我还尝试了以下(非按时间顺序)

sed 's/\r\n/\r/g' new.tsv

sed 's/\\N/NULL/g' new.tsv

sed 's/\\//' 

sed 's/\\N/\r/' 

sed -n 's/\n/\r/' 

sed -n 's/\\n/\r/' 

sed 's/\N/\r/' 

sed 's/\\N/NULL/' 

sed 's/\N/NULL/' 

sed 's/\r//' 

sed 's/\N/NULL/' 

sed 's/\\N/NULL/' 

sed 's/\\//' 

sed 's/\N/\r/g' new.tsv

sed 's/\N/NULL/g' new.tsv

sed 's/\N/NULL/g' new.tsv  

这些都没有奏效。当我使用 LibreOffice 的预览对话框查看时,它似乎可以滚动浏览内容并将它们格式化为表格。

我查看了 this 关于文字换行错误和 this question about using copy 的问题。

我不明白插入错误的“字节”是什么意思。

数据预览:https://imgur.com/JPhHB52

更新:运行 sed 's/\r/CR was here/g' myfile.tsv | grep 'CR was here' 并返回两个结果

【问题讨论】:

  • sed 's/\r/CR was here/g' myfile.tsv | grep 'CR was here' 有结果吗?同时提供你实际的copy 命令。
  • 每次使用 sed 生成文件都没有错误。我没有使用grep 进行搜索。我缺少回车的不同语法吗?我使用的是客户端\copy,我会更新帖子。
  • 好的,有一个相对简单的方法可以重现错误:echo -e 'aaa\nbbb\rccc' > foo.tsv ; psql -c 'create table foo(x text)' -c '\copy foo from foo.tsv' -c 'drop table foo;'(如果您的数据库中已经有宝贵的foo 表,请小心)让我们尝试找到解决方法...
  • echo -e 'aaa\nbbb\rccc' > foo.tsv ; sed -i 's/\r/\\r/g' foo.tsv ; psql -c 'create table foo(x text)' -c "\copy foo from foo.tsv" -c 'table foo' -c 'drop table foo;' 修复了错误。我不知道为什么\r 序列没有被copy 命令翻译成CR,如文档中所述。但这是另一个问题。
  • 不要混淆\N\n

标签: sql linux postgresql pgadmin


【解决方案1】:

这可以通过以下方式解决: sed 's/\r//\\r/g' myfile.tsv > myfile_copy.tsv

显然需要转义两次。

@Abelisto 指出了这条评论中出现的错误:

好的,有一个比较简单的方法可以重现错误:echo -e 'aaa\nbbb\rccc' > foo.tsv ; psql -c 'create table foo(x text)' -c '\copy foo from foo.tsv' -c 'drop table foo;'(如果你 您的数据库中已经有有价值的 foo 表)让我们尝试找到如何 修复它...

也很有帮助:

echo -e 'aaa\nbbb\rccc' > foo.tsv ; sed -i 's/\r/\\r/g' foo.tsv ; psql -c 'create table foo(x text)' -c "\copy foo from foo.tsv" -c 'table foo' -c 'drop table foo;' 修复了错误。我不知道为什么 \r 序列不会通过复制命令转换为 CR,如 文档。但这是另一个问题。 ——Abelisto

【讨论】:

    猜你喜欢
    • 2018-09-29
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多