postgresql - 在 PostgreSQL 中使用 COPY FROM 加载带有 TIME ZONE 的 NULL TIMESTAMP

标签 postgresql csv

我有一个 CSV 文件,我正尝试使用 COPY FROM 命令将其加载到 PostgreSQL 9.2.4 数据库中。特别是有一个允许为空的时间戳字段,但是当我加载“空值”(实际上只是“”)时,我收到以下错误:

ERROR:  invalid input syntax for type timestamp with time zone: ""

CSV 文件示例如下所示:

id,name,joined
1,"bob","2013-10-02 15:27:44-05"
2,"jane",""

SQL 如下所示:

CREATE TABLE "users"
(
    "id" BIGSERIAL NOT NULL PRIMARY KEY,
    "name" VARCHAR(255),
    "joined" TIMESTAMP WITH TIME ZONE,
);

COPY "users" ("id", "name", "joined")
    FROM '/path/to/data.csv'
    WITH (
        ENCODING 'utf-8',
        HEADER 1,
        FORMAT 'csv'
    );

根据documentation , null 值应由不能包含引号字符的空字符串表示,在本例中为双引号 ("):

NULL

Specifies the string that represents a null value. The default is \N (backslash-N) in text format, and an unquoted empty string in CSV format. You might prefer an empty string even in text format for cases where you don't want to distinguish nulls from empty strings. This option is not allowed when using binary format.

Note: When using COPY FROM, any data item that matches this string will be stored as a null value, so you should make sure that you use the same string as you used with COPY TO.

我已经尝试了选项 NULL '' 但这似乎没有任何影响。请指教!

最佳答案

没有引号的空字符串可以正常工作:

id,name,joined
1,"bob","2013-10-02 15:27:44-05"
2,"jane",

select * from users;
id | name |         joined
----+------+------------------------
 1 | bob  | 2013-10-03 03:27:44+07
 2 | jane |

也许使用 sed 将 ""替换为空字符串会更简单。

关于postgresql - 在 PostgreSQL 中使用 COPY FROM 加载带有 TIME ZONE 的 NULL TIMESTAMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28621561/

相关文章:

postgresql - 在 Docker 中使用 Sqoop 导入 PostgreSQL

python - 无法在 Python 中写入 .csv 文件中的所有行?

c# - NPOI 支持 CSV/TSV?

r - 合并多个 CSV 文件并删除 R 中的重复项

javascript - 在javascript中读取CSV文件时如何处理空行

python - 使用python返回excel中两个不同文件中两列之间的差异

java - 返回 "null"字符串的 Postgresql 文本

postgresql - 使用 Anorm 执行更新返回 PSQLException : The column index is out of range: 2, 列数:1

java - 运行子查询决定是否跳过外部查询,如果跳过不发送参数?

sql - FALSE 和 TRUE 与 NULL 和 TRUE