【问题标题】:How to export the data in CSV format of Redshift cluster using psql如何使用 psql 以 CSV 格式导出 Redshift 集群的数据
【发布时间】:2023-03-23 15:45:01
【问题描述】:

下面是我在 Linux Shell Script 中尝试过的。

psql "dbname=dbname host=host user=user password=password port=port" -A -F ',' -c "select * from table" > /path/sql_output.csv

虽然上面的命令给了我 csv 格式的输出,但它不是一个好的解决方案。因为这种方法在很多情况下会生成损坏的 CSV 文件,例如当字段包含引号和逗号时。

我尝试了另一种方法,使用 COPY 命令,但出现错误。

psql "dbname=dbname host=host user=user password=password port=port" -c "COPY ( SELECT * FROM TABLE ORDER BY id limit 10 ) TO STDOUT WITH CSV HEADER " > /path/sql_output.csv

任何帮助将不胜感激。

注意:我不是这里的超级用户。

【问题讨论】:

  • Postgres 还是 Redhift?这是两个非常不同的事情。还有什么你得到的错误?
  • 这是为 Redshift 准备的。错误:“(”或附近的语法错误。
  • As documented in the manual Redshift 不支持查询copy 命令
  • @a_horse_with_no_name 任何替代方法?
  • 只需先创建一个(临时)表,然后将其用于COPY

标签: csv amazon-redshift psql


【解决方案1】:

要将数据导出到本地文件,您可以使用 psql 客户端将输出重定向到使用 \o file_redirect.csv 的文件:

\a    -- aligned/unaligned
\f ,  -- field separator , (default is |)
\t    -- show rows on/off
\o /tmp/sql_output.csv -- output file

然后:

select * from table;

【讨论】:

    猜你喜欢
    • 2012-10-20
    • 2018-08-08
    • 2016-04-10
    • 1970-01-01
    • 2017-03-07
    • 2016-11-14
    • 1970-01-01
    • 2021-05-23
    • 2013-06-25
    相关资源
    最近更新 更多