开发者

CSV import via COPY FROM won't work with NULL values contained

I've a CSV file and want to import this via the Postgres command "COPY x FROM y" in my database. This database has two columns: a column called "id" (serial, NULL not allowed) and a "value" column (varchar, NULL is allowed and a constraint is applied). I'll provide the short create script for the test database below. The CSV is obtained by exporting it from an OpenOffice spreadsheet. The CSV content is also provided at the end.

My problem:

When executing the following command ...

COPY mytable FROM '/tmp/testdata.csv' CSV HEADER NULL AS E'\N';

... I'm getting this error message:

ERROR:  value for domain car_manufacturer violates check constraint "car_manufacturer_format"
CONTEXT:  COPY mytable, line 3, column value: "\N"

********** Error **********

ERROR: value for domain car_manufacturer violates check constraint "car_manufacturer_format"
SQL state: 23514
Context: COPY mytable, line 3, column value: "\N"

Here is the content of the CSV file (exported directly from OpenOffice, so it should conform to the standards):

"ID","value"
1,"Mercedes"
2,开发者_JS百科"\N"

Obviously, Postgres has some problems in understanding that the "\N" in my CSV file is meant to be a NULL value (which is actually allowed in this column). First, it is checking the whole string "\N" against the constraint. As it does not conform to the RegexPattern defined in the constraint, it will provide the error mentioned above. I tried several modifications of my COPY-FROM-Command but none of these led to a successful import.

In short: I need a solution for allowing Postgres me to import a csv via the copy-from-command and, moreover, being allowed to have "\N"-entries in string/varchar columns representing NULL-values.

I really need to do it with the Copy command because I want to automate the import process by using a script. When using phpPGAdmin for importing the CSV everything works fine. This is due to the fact, that phpPgAdmin is sending INSERT-Commands to the postgres server. So it is obviously not using the "copy from"-command...

Additional info:

Here is the create script for the database:

CREATE DOMAIN car_manufacturer AS VARCHAR (100) CONSTRAINT car_manufacturer_format CHECK
(VALUE SIMILAR TO '[A-Z][A-Za-z]+|');
COMMENT ON DOMAIN car_manufacturer IS 'The name of a car manufacturer (e.g, Ford, BMW, Chrysler, Mercedes etc.)';

CREATE TABLE mytable
(
  "ID" serial NOT NULL,
  "value" car_manufacturer,
  CONSTRAINT pk_mytable PRIMARY KEY ("ID")
)
WITH (
  OIDS=FALSE
);
ALTER TABLE mytable OWNER TO postgres;

Thanks a lot in advance!

Cheers, Philipp

PS: My example is boiled down in order to show you the actual problem. So, please, don't be bothered by the triviality of this example :-)


I usually don't use quoting for character fields and have a different delimiter to avoid difficulty with embedded commas, e.g. '|' when exporting open office to csv. Then on import specify the delimiter and also escape the backslash to specify null E'\\N'.


"\N" is a value that can be interpreted as a "New line" command.

Try to use another character, like "@".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜