From: David G Johnston <david.g.johnston@gmail.com>
To: pgsql-bugs@postgresql.org
Subject: Re: pg import text data to not null table comma error but semicolon right
Date: Fri, 27 Jun 2014 06:33:56 -0700 (PDT)
Message-ID: <1403876036058-5809539.post@n5.nabble.com> (raw)
In-Reply-To: <494c8a89.86fe.146dc0e8ec1.Coremail.wang1352083@163.com>
References: <21c10616.195a8.146d82287df.Coremail.wang1352083@163.com>
<CAJghg4KA_XBLgo8nEV+pgZrTaNxbZyX2KL0xcKGHRKVcEc8CBw@mail.gmail.com>
<494c8a89.86fe.146dc0e8ec1.Coremail.wang1352083@163.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
王学敏 wrote
> tks
> if import data with csv there error
> like:
>
> \copy t(d1,d2,d3) from 'comma.csv' with delimiter ',' csv ;
> ERROR: null value in column "d2" violates not-null constraint
> DETAIL: Failing row contains (e, null, g).
>
> CONTEXT: COPY t, line 2: "e,,g"
>
> and there right
> \copy t(d1,d2,d3) from 'comma.csv' with delimiter ',' ;
> demo_pg94=>
In CSV mode the empty-string is converted into NULL whereas in non-CSV mode
the empty string is left as an empty string.
http://www.postgresql.org/docs/9.3/interactive/sql-copy.html
see "NULL"
You are only getting an error in the first case because of the definition of
t.d2 includes a NOT NULL constraint.
You can either remove the constraint or modify the \copy command to not
treat empty strings as NULL is CSV mode. It kinda depends on the real data
which option is better. Typically I'd drop NOT NULL and treat the copy
target as a staging table; then when migrating to the live table I'd
COALESCE the NULL into a meaningful default.
David J.
--
View this message in context: http://postgresql.1045698.n5.nabble.com/pg-import-text-data-to-not-null-table-comma-error-but-semico...
Sent from the PostgreSQL - bugs mailing list archive at Nabble.com.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: david.g.johnston@gmail.com
Subject: Re: pg import text data to not null table comma error but semicolon right
In-Reply-To: <1403876036058-5809539.post@n5.nabble.com>
References: <21c10616.195a8.146d82287df.Coremail.wang1352083@163.com> <CAJghg4KA_XBLgo8nEV+pgZrTaNxbZyX2KL0xcKGHRKVcEc8CBw@mail.gmail.com> <494c8a89.86fe.146dc0e8ec1.Coremail.wang1352083@163.com> <1403876036058-5809539.post@n5.nabble.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox