Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

Can't use COPY FROM to load an "E" into a CHAR(1) column in PostgreSQL from a CSV file

My table looks like this:

CREATE TABLE IF NOT EXISTS metering.service_point (
service_point_id int GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY,
service_point_number varchar (20) NULL,
property_id int NULL,
fuel_type char (1) NULL,
ldz_id smallint NULL,
gsp_id smallint NULL,
supply_start_date timestamp NULL,
supply_end_date timestamp NULL);

I'm inserting data from a CSV that looks like this:

service_point_number, property_id, fuel_type, gsp_id, ldz_id, supply_start_date
'1717209598220',1,'E',8,,'20180428'

Using this command:

COPY metering.service_point (service_point_number, property_id, fuel_type, gsp_id, ldz_id, supply_start_date)
FROM 'C:BillingDataservice_point.csv'
DELIMITER ','
CSV HEADER;

This is the fourth table in a batch loading process, and the first three went fine. This one fails with: value too long for type character(1)

However, if I copy the contents DIRECTLY from the CSV and paste it into an INSERT query, like this:

INSERT INTO metering.service_point (service_point_number, property_id, fuel_type, gsp_id, ldz_id, supply_start_date)
SELECT '1717209598220',1,'E',8,NULL,'20180428';

Then it works fine.

My assumption is that this must be a bug. I can work around it by setting the value to NULL, and then fixing it up later. I can also workaround the problem by changing the data type for fuel_type, but this seems a little sad.

I guess I'm just missing something simple, maybe something unicode related - like the E in my CSV actually counts as 2 characters for some strange reason?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...