public inbox for [email protected]  
help / color / mirror / Atom feed
Using \copy to populate a table
2+ messages / 2 participants
[nested] [flat]

* Using \copy to populate a table
@ 2026-03-15 07:11 rob stone <[email protected]>
  2026-03-17 02:16 ` Re: Using \copy to populate a table Peter Kleiner <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: rob stone @ 2026-03-15 07:11 UTC (permalink / raw)
  To: pgsql-general

psql (18.3 (Debian 18.3-1+b1))
Type "help" for help.

Debian OS is forky.

When setting up a new development database, the following occurred:-

applntestdb=> \copy forms_table (item_type, navgn_refn, html_name,
table_key, navgn_bar, rows_page, forward_to, second_to, tertiary_to,
active_item, super_only, system_admin, inserted_by) from
'/home/postgres/loadfiles/formstable.txt' header delimiter '|';
ERROR:  invalid input syntax for type smallint: "null"
CONTEXT:  COPY forms_table, line 2, column rows_page: "null"
applntestdb=>

This is what is on line 2 of the file:-
C|0|pageloader|||null|null|null|null|t|f|f|0

The test below was simplified to just choose one SMALLINT column. 

applntestdb=> insert into forms_table (item_type, navgn_refn,
html_name, rows_page, active_item,
inserted_by) values ('C', 0, 'pageloader', null, TRUE, 0);
INSERT 0 1
applntestdb=> delete from forms_table where item_type = 'C';
DELETE 1
applntestdb=> insert into forms_table (item_type, navgn_refn,
html_name, rows_page, active_item,
inserted_by) values ('C', 0, 'pageloader', '', TRUE, 0);
ERROR:  invalid input syntax for type smallint: ""
LINE 2: inserted_by) values ('C', 0, 'pageloader', '', TRUE, 0)
                                                   ^
                                                   
So, psql is quite happy to insert null into a column defined as
smallint, but when you use the \copy mechanism to populate a table it
pulls an error.

How do we fix this? Alter all the nulls to zeroes in the file or write
a program to dissect the file and create individual insert statements?

Has anybody else had this problem and if so what was the solution?

Thanks,
Rob






^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Using \copy to populate a table
  2026-03-15 07:11 Using \copy to populate a table rob stone <[email protected]>
@ 2026-03-17 02:16 ` Peter Kleiner <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Peter Kleiner @ 2026-03-17 02:16 UTC (permalink / raw)
  To: ; +Cc: pgsql-general

On Sun, Mar 15, 2026 at 3:12 AM rob stone <[email protected]> wrote:

> psql (18.3 (Debian 18.3-1+b1))
> Type "help" for help.
>
> Debian OS is forky.
>
> When setting up a new development database, the following occurred:-
>
> applntestdb=> \copy forms_table (item_type, navgn_refn, html_name,
> table_key, navgn_bar, rows_page, forward_to, second_to, tertiary_to,
> active_item, super_only, system_admin, inserted_by) from
> '/home/postgres/loadfiles/formstable.txt' header delimiter '|';
> ERROR:  invalid input syntax for type smallint: "null"
> CONTEXT:  COPY forms_table, line 2, column rows_page: "null"
> applntestdb=>
>
> This is what is on line 2 of the file:-
> C|0|pageloader|||null|null|null|null|t|f|f|0
>
> The test below was simplified to just choose one SMALLINT column.
>
> applntestdb=> insert into forms_table (item_type, navgn_refn,
> html_name, rows_page, active_item,
> inserted_by) values ('C', 0, 'pageloader', null, TRUE, 0);
> INSERT 0 1
> applntestdb=> delete from forms_table where item_type = 'C';
> DELETE 1
> applntestdb=> insert into forms_table (item_type, navgn_refn,
> html_name, rows_page, active_item,
> inserted_by) values ('C', 0, 'pageloader', '', TRUE, 0);
> ERROR:  invalid input syntax for type smallint: ""
> LINE 2: inserted_by) values ('C', 0, 'pageloader', '', TRUE, 0)
>                                                    ^
>
> So, psql is quite happy to insert null into a column defined as
> smallint, but when you use the \copy mechanism to populate a table it
> pulls an error.
>
> How do we fix this? Alter all the nulls to zeroes in the file or write
> a program to dissect the file and create individual insert statements?
>
> Has anybody else had this problem and if so what was the solution?
>
>
Caveat: I'm a hack, I rarely respond to questions here, I have limited
knowledge about PGSQL, databases in general, and generally hesitate to
chime in where my betters are wont to dwell.

That said... If it was me, I would at least try switching this

 \copy forms_table (item_type, navgn_refn, html_name,
table_key, navgn_bar, rows_page, forward_to, second_to, tertiary_to,
active_item, super_only, system_admin, inserted_by) from
'/home/postgres/loadfiles/formstable.txt' header delimiter '|';

with this

 \copy forms_table (item_type, navgn_refn, html_name,
table_key, navgn_bar, rows_page, forward_to, second_to, tertiary_to,
active_item, super_only, system_admin, inserted_by) from
'/home/postgres/loadfiles/formstable.txt' with header delimiter '|' NULL as
'null';

In other words, your CSV file seems to be outputting the string 'null' for
NULL values.

Doing it this way, however, would mean that any of your fields whose string
values might be "null" would be interpreted as NULL values.

The fact that no one has already offered this as an approach makes me
skeptical, but perhaps you could give it a shot to see if it works.

Pete


^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-03-17 02:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-03-15 07:11 Using \copy to populate a table rob stone <[email protected]>
2026-03-17 02:16 ` Peter Kleiner <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox