pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedpg import text data to not null table comma error but semicolon right
6+ messages / 4 participants
[nested] [flat]
* pg import text data to not null table comma error but semicolon right
@ 2014-06-26 12:17 王学敏 <15810475206@163.com>
0 siblings, 0 replies; 6+ messages in thread
From: 王学敏 @ 2014-06-26 12:17 UTC (permalink / raw)
To: pgsql-bugs
Hi,
when i imort data from csv to table ,there may be some error
create table t(d1 text not null,d2 text not null,d3 text not null);
\copy t(d1,d2,d3) from 'comma.text' with delimiter ',';
ERROR: missing data for column "d2"
CONTEXT: COPY t, line 1: "a;b;c"
\copy t(d1,d2,d3) from 'semicolon.text' with delimiter ';' ;
--no error
cat comma.text
a,b,c
e,,f
cat semicolon.text
a;b;c
e;;f
^ permalink raw reply [nested|flat] 6+ messages in thread
* pg import text data to not null table comma error but semicolon right
@ 2014-06-26 12:24 王学敏 <wang1352083@163.com>
2014-06-26 12:52 ` Re: pg import text data to not null table comma error but semicolon right David G Johnston <david.g.johnston@gmail.com>
2014-06-26 12:57 ` Re: pg import text data to not null table comma error but semicolon right Matheus de Oliveira <matioli.matheus@gmail.com>
0 siblings, 2 replies; 6+ messages in thread
From: 王学敏 @ 2014-06-26 12:24 UTC (permalink / raw)
To: pgsql-bugs
Hi,
when i imort data from csv to table ,there may be some error
create table t(d1 text not null,d2 text not null,d3 text not null);
\copy t(d1,d2,d3) from 'comma.text' with delimiter ',';
ERROR: missing data for column "d2"
CONTEXT: COPY t, line 1: "a;b;c"
\copy t(d1,d2,d3) from 'semicolon.text' with delimiter ';' ;
--no error
cat comma.text
a,b,c
e,,f
cat semicolon.text
a;b;c
e;;f
来自网易手机号码邮箱了解更多
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: pg import text data to not null table comma error but semicolon right
2014-06-26 12:24 pg import text data to not null table comma error but semicolon right 王学敏 <wang1352083@163.com>
@ 2014-06-26 12:52 ` David G Johnston <david.g.johnston@gmail.com>
1 sibling, 0 replies; 6+ messages in thread
From: David G Johnston @ 2014-06-26 12:52 UTC (permalink / raw)
To: pgsql-bugs
王学敏 wrote
> Hi,
> when i imort data from csv to table ,there may be some error
> create table t(d1 text not null,d2 text not null,d3 text not null);
>
> \copy t(d1,d2,d3) from 'comma.text' with delimiter ',';
> ERROR: missing data for column "d2"
> CONTEXT: COPY t, line 1: "a;b;c"
>
> \copy t(d1,d2,d3) from 'semicolon.text' with delimiter ';' ;
> --no error
> cat comma.text
> a,b,c
> e,,f
>
> cat semicolon.text
> a;b;c
> e;;f
Apparently the comma.text file you are trying to import and the one you are
cat'ing are not the same file...
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
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: pg import text data to not null table comma error but semicolon right
2014-06-26 12:24 pg import text data to not null table comma error but semicolon right 王学敏 <wang1352083@163.com>
@ 2014-06-26 12:57 ` Matheus de Oliveira <matioli.matheus@gmail.com>
2014-06-27 06:41 ` Re: pg import text data to not null table comma error but semicolon right 王学敏 <wang1352083@163.com>
1 sibling, 1 reply; 6+ messages in thread
From: Matheus de Oliveira @ 2014-06-26 12:57 UTC (permalink / raw)
To: 王学敏 <wang1352083@163.com>; +Cc: pgsql-bugs
On Thu, Jun 26, 2014 at 9:24 AM, 王学敏 <wang1352083@163.com> wrote:
> \copy t(d1,d2,d3) from 'comma.text' with delimiter ',';
> ERROR: missing data for column "d2"
> CONTEXT: COPY t, line 1: "a;b;c"
>
>
This error happens because the line uses semicolon (";") as delimiter and
you used comma (",") on COPY command, so PG thinks "a;b;c" is all value for
column d1, and so there is no values for d2 and d3.
Looks like you are using a file different from the one you showed.
\copy t(d1,d2,d3) from 'semicolon.text' with delimiter ';' ;
> --no error
> cat semicolon.text
> a;b;c
> e;;f
>
That should be fine indeed. On the second line d2 will be imported as empty
string, not NULL, hence no error.
If you wanted this to be considered NULL, you should be using CSV format,
like this:
\copy t(d1,d2,d3) FROM 'semicolon.text' WITH CSV DELIMITER ';' ;
And that would give the error you probably expect:
ERROR: 23502: null value in column "d2" violates not-null constraint
DETAIL: Failing row contains (e, null, f).
CONTEXT: COPY t, line 2: "e;;f"
So. No bug here.
Regards,
--
Matheus de Oliveira
Analista de Banco de Dados
Dextra Sistemas - MPS.Br nível F!
www.dextra.com.br/postgres
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: pg import text data to not null table comma error but semicolon right
2014-06-26 12:24 pg import text data to not null table comma error but semicolon right 王学敏 <wang1352083@163.com>
2014-06-26 12:57 ` Re: pg import text data to not null table comma error but semicolon right Matheus de Oliveira <matioli.matheus@gmail.com>
@ 2014-06-27 06:41 ` 王学敏 <wang1352083@163.com>
2014-06-27 13:33 ` Re: pg import text data to not null table comma error but semicolon right David G Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: 王学敏 @ 2014-06-27 06:41 UTC (permalink / raw)
To: Matheus de Oliveira <matioli.matheus@gmail.com>; +Cc: pgsql-bugs
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=>
At 2014-06-26 08:57:32, "Matheus de Oliveira" <matioli.matheus@gmail.com> wrote:
On Thu, Jun 26, 2014 at 9:24 AM, 王学敏 <wang1352083@163.com> wrote:
\copy t(d1,d2,d3) from 'comma.text' with delimiter ',';
ERROR: missing data for column "d2"
CONTEXT: COPY t, line 1: "a;b;c"
This error happens because the line uses semicolon (";") as delimiter and you used comma (",") on COPY command, so PG thinks "a;b;c" is all value for column d1, and so there is no values for d2 and d3.
Looks like you are using a file different from the one you showed.
\copy t(d1,d2,d3) from 'semicolon.text' with delimiter ';' ;
--no error
cat semicolon.text
a;b;c
e;;f
That should be fine indeed. On the second line d2 will be imported as empty string, not NULL, hence no error.
If you wanted this to be considered NULL, you should be using CSV format, like this:
\copy t(d1,d2,d3) FROM 'semicolon.text' WITH CSV DELIMITER ';' ;
And that would give the error you probably expect:
ERROR: 23502: null value in column "d2" violates not-null constraint
DETAIL: Failing row contains (e, null, f).
CONTEXT: COPY t, line 2: "e;;f"
So. No bug here.
Regards,
--
Matheus de Oliveira
Analista de Banco de Dados
Dextra Sistemas - MPS.Br nível F!
www.dextra.com.br/postgres
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: pg import text data to not null table comma error but semicolon right
2014-06-26 12:24 pg import text data to not null table comma error but semicolon right 王学敏 <wang1352083@163.com>
2014-06-26 12:57 ` Re: pg import text data to not null table comma error but semicolon right Matheus de Oliveira <matioli.matheus@gmail.com>
2014-06-27 06:41 ` Re: pg import text data to not null table comma error but semicolon right 王学敏 <wang1352083@163.com>
@ 2014-06-27 13:33 ` David G Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 6+ messages in thread
From: David G Johnston @ 2014-06-27 13:33 UTC (permalink / raw)
To: pgsql-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
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2014-06-27 13:33 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-06-26 12:17 pg import text data to not null table comma error but semicolon right 王学敏 <15810475206@163.com>
2014-06-26 12:24 pg import text data to not null table comma error but semicolon right 王学敏 <wang1352083@163.com>
2014-06-26 12:52 ` David G Johnston <david.g.johnston@gmail.com>
2014-06-26 12:57 ` Re: pg import text data to not null table comma error but semicolon right Matheus de Oliveira <matioli.matheus@gmail.com>
2014-06-27 06:41 ` Re: pg import text data to not null table comma error but semicolon right 王学敏 <wang1352083@163.com>
2014-06-27 13:33 ` Re: pg import text data to not null table comma error but semicolon right David G Johnston <david.g.johnston@gmail.com>
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