【发布时间】:2017-08-03 05:16:06
【问题描述】:
我有一个 postgres 9.6 表,它有一个文本列,其中包含 HTML 格式的电子邮件内容。所以里面有各种各样的人物。我需要将其导出并导入 Redshift 数据库。我尝试了带有各种分隔符的 COPY 命令 - |,~ ^ 但由于这些字符位于文本数据类型中,它一直失败。
我尝试使用带引号的 CSV,但这会导致文本数据跨越多行。
我运行的一些命令 -
-- 从 Postgres 导出
psql -d messsagedb -c "copy (SELECT body FROM messages limit 3) to STDOUT (DELIMITER '^')
-- 在 Redshift 中导入
COPY public.messages FROM 's3://xxx/messages_stub2.txt' iam_role 'arn:aws:iam::xxx' delimiter '^';
这会失败并显示消息 - 时间戳无效(因为正文中有“^”)
--使用二进制或csv从postgres导出
psql -d messagedb -c "copy (SELECT body FROM messages limit 3) to STDOUT (format csv) "
-- 复制到 Redshift 失败并出现错误 - “Delimited value is missing end quote”
下面的示例数据。当我使用 CSV 时,电子邮件正文跨越多行。因此,即使我在 COPY 中的 SELECT 语句有一个 LIMIT 1,输出也会生成大约 50 行
"86b599b2-e0fb-4f16-b026-6aedefd52e76","3266841",,"July 15th Skills Camp Update","
<div>
<div style=""clear: both;padding-top: 15px"">
<table cellpadding=""1"" cellspacing=""1"" width=""100%"">
<tbody>
<tr>
<td>
<img alt="""" src=""https://www.frontrush.com//userfiles/591/email%xx.jpg"" style=""width: 933px; height: 350px;"">
<table border=""1"" cellpadding=""1"" cellspacing=""1"" style=""width: 933px;"">
<tbody>
<tr>
<td style=""text-align: center;"">
<br>
<span style=""font-size:72px;""><span style=""font-family:Tahoma,Geneva,sans-serif;""><u>Methodist University Football</u></span></span><br>
<br>
<br>
<br>
<font><span style=""font-size: 48px;"">At this time, the online registration has been closed for the July 15th Camp! Walk-ups are still accepted and welcomed! We look forward to seeing you here.</span></font><br>
关于可以使用的稀有单字节分隔符有什么想法吗?还有其他我可以使用的 COPY 选项吗?不幸的是,pg_dump 不是一个选项,因为我需要在这个表上运行 SELECT。
谢谢 壁画
【问题讨论】:
-
您似乎遗漏了示例数据、输出、错误消息等
-
对这个问题有什么建议吗?谢谢!
-
看起来 Redshift 的 CSV 解析器可能有问题,不能正确处理换行符。您需要使用外部工具来加载 CSV。
标签: postgresql copy export