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 1w1i5C-000XM3-1r for pgsql-general@arkaria.postgresql.org; Sun, 15 Mar 2026 09:48:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w1i5B-0041b1-2H for pgsql-general@arkaria.postgresql.org; Sun, 15 Mar 2026 09:48:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w1i5B-0041as-10 for pgsql-general@lists.postgresql.org; Sun, 15 Mar 2026 09:48:26 +0000 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w1i59-00000000FUB-2grb for pgsql-general@postgresql.org; Sun, 15 Mar 2026 09:48:25 +0000 Received: by mail-ot1-x336.google.com with SMTP id 46e09a7af769-7d1872504cbso3759725a34.0 for ; Sun, 15 Mar 2026 02:48:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1773568101; cv=none; d=google.com; s=arc-20240605; b=Y86wY02MqH4xhO4MXVh5zmzsGOnkPBezhUv/1zuOsKN6UAJIUPYD8XovzAqE0YijC2 C2RNWvIcSSW3NOlqLe+6tpqQN3ySjMURknQsVHTS+ZvvYAHOvBpd0U5r873ODpkIcuL+ WHNpbYwT7JIjQ9OlSIoPSCERn1Tt3Hbf6DXfJZAmg40+fPiJnc45q8+TkodcwcjXJQIT lPcTcmxAR2GLEryrEGqtN6szNSyIHM2jcKsQxGz1GJ/T+1PV+YTTPvSGijIjCe1q7vrG WZ9tiWGRLBzzJjtVO3Ht5MYodjvEI7tl8dJHjvSNS+yPk3bhwccgFxxJ0LwFxzo8H73N Q3Ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=6FdMFCTI6XkEFkYbTfIGKfOcBqC+8A2HF/p1MJYgHQs=; fh=lvdqO9KmfXYPuvvRj1/tWW/E/r3INp6uSwxf+SqibAk=; b=bE5EuxtlW0+KWBk/DZ5m3KDscxwM+6xmeiUBSfZCyueNnoFczo/vzmWHfs5tmq6euM 6zFjkm+GRxi1u0tgAVoAuZeWFhUtvjkUNF32UVsA53aPdVcXYv/TVopyAF1CTqod+6Ph dxRYCBFJTFz8J0K5RTXUtJIgnl1SaGaOcE+b7xk/SnGp6EswXpFVeMtd2EshJzlQOpfE JjHjj/+GgslM0CmMTNpUlGYQYuD+O/Pif7RR2Wz69R0Fgi1UdlgwgPwee8UW1xUaVl9h K4EItPUY6ijJh5spAtLvOTyUKSit005NWCHNzN2OyPycBS7MUYzfW1kFLiZSb96v9APh N5Ng==; 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=1773568101; x=1774172901; darn=postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=6FdMFCTI6XkEFkYbTfIGKfOcBqC+8A2HF/p1MJYgHQs=; b=GpyaopoV+68iFJwl+Xio/7pGF1Crme7PDBmJleoZimZ99kSHgOxefW/ZTQxqkfRd4W MDTMW2WZEA/z6UyOz9obXHv94Z1aCm4YpSIhmhTvNeqwQsBZc3T0WC11OYxU/m9FCrg8 Z5hGA5Vzr9c3rXdy7XcnfVa0BEBMjAemoHM9vfG20XRAzAS9AYENz67iqi/T1Rbnvi/3 fxfJHUPpiGL8SgazXyITk+KZ0b5d/yN+Wg/ry3+Bnag0W2wysxde+gmpjsjlSkpVwnb6 UlivTAuuuDBEf2YCREhYfe1RyXtvzjzvTnwrNDFc3lPY/R8hrIsisfoP6FJfcu+0BGov OKHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773568101; x=1774172901; h=to: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=6FdMFCTI6XkEFkYbTfIGKfOcBqC+8A2HF/p1MJYgHQs=; b=epYu+30/ZezO651xeeABASnWPQaMRA4qJdv/5pc5xoCvboo/OExMOGCCECG/wrdcqc 5maUdPEbwnyGzfHl2BTmfIsTpeov8SgcwViuQwrqjBIc3yuGn380qm1FWWvImUAoLa64 XMSy4zh6KhgkLfd5yIQpuUMYzNMAlXB9uwS6q3ubIjncdKXyLvLeeFw6vTMy10cDnKrE VjZjOotO+8lT2X4nJ9FTbNV973Qh9/WjnIuapN8k/x3HYnJr8EcPzghTbwRQzEbDOOF9 7pGnKj1TyQOJFDT/6KoYc2OEgXQWsAqwfL/FD4p8Otxczy5vpVO/DNVDwPOFmptL85xm GUUA== X-Gm-Message-State: AOJu0YxLwbwWCqLYCk9+5APUWOmtlUI80crIX6FZlNsBP2NvVI9i8WG5 Lsn1JqSuKUbAeQb2MA5Y6ktKulERwd/bvTctq3WquOqF+0u8M4cVHQ4vnVALWjpUQRHjl3I6AO2 J8EvmjzywdrvfkX5Yl8CQUWIBAtor5uvmyJap X-Gm-Gg: ATEYQzz4Ldy/2HXlY45BqWPNuAQXIFkQDnvdnNHiZui/wjCN/Hg0se6czqWVaLsb3n5 b2YWxJKCmWCkgirh0iqDrDFwTkCv+k8e0V5WuBlrF4CQTKYULRHPrnASc/yeATEK9OB3+Y6R8mI TO15R+G0AnvjB21YUnjeOOslFM8ToeaM/OIfPGj6GDL324NKiiQRsoNQgwKz3QNY40epSMmk3jv X8DQZEUBoo7Xt0KbMnOdwOt3NUEKVrCMIxiyoLT8hZRu168TTFUPExSzV50vKpJUoLmhWjtIvUl oIu2MJ5e X-Received: by 2002:a05:6830:3bc5:b0:7d1:9066:26b7 with SMTP id 46e09a7af769-7d781c3fbfdmr6489337a34.6.1773568101594; Sun, 15 Mar 2026 02:48:21 -0700 (PDT) MIME-Version: 1.0 References: <43388977a1cdbde81043c690b41e0647e9b95a2b.camel@tpg.com.au> In-Reply-To: <43388977a1cdbde81043c690b41e0647e9b95a2b.camel@tpg.com.au> From: Ron Johnson Date: Sun, 15 Mar 2026 05:48:10 -0400 X-Gm-Features: AaiRm53z1wpEy9hcdmDm8C2JYQwolDINCK89IBmyO6Uj5Qwqd_RCnUe3NKh1gto Message-ID: Subject: Re: Using \copy to populate a table To: PostGreSQL MailingList Content-Type: multipart/alternative; boundary="000000000000ae3b1a064d0d00d6" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ae3b1a064d0d00d6 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? > "null" is not what COPY uses to signal a null value. I think it's \N but the best solution i to COPY TO your table to STDOUT and see what it uses as the null indicator. \copy forms_table TO STDOUT WITH (HEADER, DELIMITER '|'); --=20 Death to , and butter sauce. Don't boil me, I'm still alive. lobster! --000000000000ae3b1a064d0d00d6 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?

"null" is not what COPY uses to signa= l a null value.=C2=A0 I think it's \N but the best solution i to COPY T= O your table to STDOUT and see what it uses as the null indicator.
\copy forms_table=C2=A0 TO STDOUT WITH (HEADER,=C2=A0DELIMITER '|'= ;);


--
Death to <Redacted>, and butter sauce.
Don't boil me= , I'm still alive.
<Redacted> lobster!
--000000000000ae3b1a064d0d00d6--