PostgreSQL 的COPY
命令具有NULL 'some_string'
允许将任何字符串指定为 NULL 值的选项:https ://www.postgresql.org/docs/current/sql-copy.html
这当然需要重新导入所有表。
您的数据示例:
CSV:
"id","sourceType","name","website","location"
"1","non-commercial","John Doe",NULL,"California"
"2","non-commercial","John Doe",NULL,"California"
桌子:
CREATE TABLE import_with_null (id integer, source_type varchar(50), name varchar(50), website varchar(50), location varchar(50));
COPY
声明:
COPY import_with_null (id, source_type, name, website, location) from '/tmp/import_with_NULL.csv' WITH (FORMAT CSV, NULL 'NULL', HEADER);
测试将 NULL 字符串正确导入为 SQL NULL:
SELECT * FROM import_with_null WHERE website IS NULL;
id | source_type | name | website | location
----+----------------+----------+---------+------------
1 | non-commercial | John Doe | | California
2 | non-commercial | John Doe | | California
(2 rows)
将 NULL 字符串转换为 SQL NULL 值的重要部分是NULL 'NULL'
并且可以是任何其他值NULL 'whatever string'
。