Re: Using \copy to populate a table

From: Francisco Olarte <folarte(at)peoplecall(dot)com>
To: rob stone <floriparob(at)tpg(dot)com(dot)au>
Cc: PostGreSQL MailingList <pgsql-general(at)postgresql(dot)org>
Subject: Re: Using \copy to populate a table
Date: 2026-03-15 09:50:33
Message-ID: CA+bJJbwDP00q-fcXUaU3pXg=o4DdWwoeSGDQOUBQ1Sa363fiug@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Rob:

On Sun, 15 Mar 2026 at 08:12, rob stone <floriparob(at)tpg(dot)com(dot)au> wrote:

>
> This is what is on line 2 of the file:-
> C|0|pageloader|||null|null|null|null|t|f|f|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.
>

It does not for me, but syntax for INSERT and COPY ( \copy just does a COPY
from stdin ) is different.

> 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?
>

Have you tried a little RTFM?

specifically ....

https://www.postgresql.org/docs/18/sql-copy.html

Where somewhere it says:

NULL

Specifies the string that represents a null value. The default is \N
(backslash-N) in text format, and an unquoted empty string in CSV format.
You might prefer an empty string even in text format for cases where you
don't want to distinguish nulls from empty strings. This option is not
allowed when using binary format.

If you generate four text files I would recommend switching to \N, it will
be less painful.

Francisco Olarte.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2026-03-15 15:30:44 Re: Using \copy to populate a table
Previous Message Ron Johnson 2026-03-15 09:48:10 Re: Using \copy to populate a table