Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w2Jz8-000Bem-1g for pgsql-general@arkaria.postgresql.org; Tue, 17 Mar 2026 02:16:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w2Jz7-00F66d-18 for pgsql-general@arkaria.postgresql.org; Tue, 17 Mar 2026 02:16:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w2Jz6-00F65q-2t for pgsql-general@lists.postgresql.org; Tue, 17 Mar 2026 02:16:41 +0000 Received: from mail-dl1-x1236.google.com ([2607:f8b0:4864:20::1236]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w2Jz3-000000006zN-2RVE for pgsql-general@postgresql.org; Tue, 17 Mar 2026 02:16:40 +0000 Received: by mail-dl1-x1236.google.com with SMTP id a92af1059eb24-1273349c56bso7814414c88.0 for ; Mon, 16 Mar 2026 19:16:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1773713798; cv=none; d=google.com; s=arc-20240605; b=borSrvystwh5VX4si8jLT387Mm+9NlhcYgl6KW+XwVZJRMTg+gT5UN4vQLtFz2dfZT N/6WZ1/+GaMklIBo3++LyAYpNfAwlUJTvVGsr5jrLzzzdtBxqBDTjw+n2xDcQ5Tb6Jjg xRJvVZ9+vOIFQsf0+KWZRGA+/ME55G6OE2LNE7HXiBUFOnOaZ3Z2QFztZzx8y9UqqkuL Pj0AvGHuyDDX62bXjTrw9YPTpGe7mNJ+0Pa20sy8zh0Rl7dm4ev12w5C2z/R6laqHrJs zsY32V/szSMxehFVfnOWQbq0cGcJqXoiJhdIt5nVZ/HDy5S8WZNn9PTbpNBUMiZSj8AI p6/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:subject:message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=LOctOg0Qg+LxNyAjLJsMiB+F2lzhWdAiOwU9rjmj0bk=; fh=1wLNPFg1tQxHDYA879DXhlNNPVYur+o7XpFd7Aj7oLw=; b=bTfbduNpZiVXN40dqB3IJUBlAdoI7v6q7KlEdCZI4Kajh4L81m/X7li/hDcw/NToBC HHidLA0zKUA2ySHvI8TTe38+t4EQJb2xYHEbPBTy0AK92PAQOUy8XqkRTsfyd2njJkGC PAZtUaoM2K3RxvJC/IQbkbyT7X6Uhe4YRKUA33m5au90N8FcVJxOdTlWDwgIl1WnuOxL 1Zsro9jGi3wUR/vAaCc7JnBcj+Xfxs6VqpfYR66xCF3dTpAMBUkhPBYc2//JjMUoEzmF AwkxtrYra6K1Q14B6ktX/cOpYKrtLd5W9k4VyS47pz1uc9/vAwQYEU4PIKNA1QdmNv54 T3aw==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773713798; x=1774318598; darn=postgresql.org; h=cc:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=LOctOg0Qg+LxNyAjLJsMiB+F2lzhWdAiOwU9rjmj0bk=; b=WqGQSDI2gXCICHlmmXrNZG5vyVU+TPcSYzDCfwpO/fE4+cXiQbij4JRVaPfRQU4E1u gFbmfsQUjlzYRI+sM1X4zNye2Y6W8MIR+pY1ihBN6cfhbn4yrFgOYp0C7iDd6VuuNWg5 +MBinO3bjs9xqhUEjzww5ppoRoH9mZ7WBmjnf7tO/wRXGN+NReujgwXzsZlfxbJREtwj KMudltfVmR2CxFNYV9069aD/ILZBxkmvaYIZO+2c3EzqdbOA4rSpt0Ob88fPkpmgMWyo DtVqKvenrtbBO4FRLfmPiW982XlcSHIjAMETWCY5rbAgZBU/wdEJ8la7IJ8DVhERyYeX CNig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773713798; x=1774318598; h=cc:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LOctOg0Qg+LxNyAjLJsMiB+F2lzhWdAiOwU9rjmj0bk=; b=T7VzqwgBw6jmGmRb510PSLJ0HcClizVZkHhBXL8/pO7Sqp0aPZw9xsDeYUQOiyX5Oy P7tgWQRsFNzFYyRcS9qF/R6035YibZm44hm/7M/eldeOj2vzugpSCAwOYccYsWC8SB5b eHBdjWdLkHggd831D+XELTkQGt/Cgz8H6D6tXS4eV5E4QSjLypWLElS7uQyKANQAIMSp jllagHWQ0KOegTW///+QFP/7gmfIU/ZlQdKj+IkKzwiUfvu76Z+QXdYsDWkyjs0iXBuI lV8bqV0DGczYtvWBlN++pO/RaD4uU5nc+nphgQlpTmLJdfUwdlle4Dtyt0nWtpUpU8Wz WHRg== X-Gm-Message-State: AOJu0YzG5vX4AMP6uiO8MEOesfCEXUO13piMaXm4AIeuIvf6e2mdK9k+ rAmANiHSB2DpK1EhPqp2vmGZAd5XCtNf+3wNOxtkwmk2kvH8R9dNhpKhpHb8SaEA6HAbJLrtnP3 lCz+fpJLFBaRb/jCs8bQfSWy6/hJyNpKiBBbsD64= X-Gm-Gg: ATEYQzy32HFPalqTa3OV/2dDwVccLXCv1g7g5nrRBUz4CLj0bCJcDMfOAuDL5qD76EZ q5YaAzuzt2M71QEZOCdx4ruIvAclQvHnSYa2mYmRlRVOQ18Zc5XVup+Qtfzz6gR+o197yMlC6Xs tslMEZFLHAW6VQViwRLgbPY9/+bcUf8t8uI99UD34jpnjhomipLnzoXyF6irSLwvSrpaX37Y2Ux BtKuigTqxTfhtdXjuF0BEkbcDscgmy8A0VyM0zfnnSc73ISKNdWC4nc5ZyRft/yUGJmfRmoVN+I jEnUNcpYTLwwqVINXsuUnpoDzon77J9fma91y9Y= X-Received: by 2002:a05:7022:439f:b0:128:ceac:6db1 with SMTP id a92af1059eb24-128f3dd132cmr6452563c88.28.1773713797757; Mon, 16 Mar 2026 19:16:37 -0700 (PDT) MIME-Version: 1.0 References: <43388977a1cdbde81043c690b41e0647e9b95a2b.camel@tpg.com.au> In-Reply-To: <43388977a1cdbde81043c690b41e0647e9b95a2b.camel@tpg.com.au> From: Peter Kleiner Date: Mon, 16 Mar 2026 22:16:26 -0400 X-Gm-Features: AaiRm51MLx66-Wp3eEA2qTHvr1qOeGb1VobwjDxnOCCSuM2QSsgE1FzxKF67VR4 Message-ID: Subject: Re: Using \copy to populate a table Cc: PostGreSQL MailingList Content-Type: multipart/alternative; boundary="000000000000d940aa064d2eec99" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000d940aa064d2eec99 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Mar 15, 2026 at 3:12=E2=80=AFAM rob stone w= rote: > 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=3D> \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=3D> > > 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=3D> 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=3D> delete from forms_table where item_type =3D 'C'; > DELETE 1 > applntestdb=3D> 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 --000000000000d940aa064d2eec99 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Sun, Mar 15, 2026 at 3:12=E2=80=AFAM r= ob stone <floriparob@tpg.com.au= > 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=3D> \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 '|&#= 39;;
ERROR:=C2=A0 invalid input syntax for type smallint: "null"
CONTEXT:=C2=A0 COPY forms_table, line 2, column rows_page: "null"=
applntestdb=3D>

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=3D> insert into forms_table (item_type, navgn_refn,
html_name, rows_page, active_item,
inserted_by) values ('C', 0, 'pageloader', null, TRUE, 0);<= br> INSERT 0 1
applntestdb=3D> delete from forms_table where item_type =3D 'C';=
DELETE 1
applntestdb=3D> insert into forms_table (item_type, navgn_refn,
html_name, rows_page, active_item,
inserted_by) values ('C', 0, 'pageloader', '', TRUE= , 0);
ERROR:=C2=A0 invalid input syntax for type smallint: ""
LINE 2: inserted_by) values ('C', 0, 'pageloader', '= 9;, TRUE, 0)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0^

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?

<= /blockquote>
=C2=A0
Caveat: I'm a hack, I rarely respond= =C2=A0to questions here, I have limited knowledge about PGSQL, databases in= general, and generally hesitate to chime in where my betters are wont to d= well.

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

=C2=A0\copy forms_table (item_t= ype, 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 delim= iter '|';

with this

=C2=A0\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/loadfile= s/formstable.txt' with header delimiter '|' NULL as 'null&#= 39;;

In other words, your CSV file seems to be out= putting the string 'null' for NULL values.=C2=A0 =C2=A0
<= br>
Doing it this way, however, would mean that any of your field= s whose string values might be "null" would be interpreted as NUL= L values.

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

Pete


--000000000000d940aa064d2eec99--