Re: Escape Python to `json[]` for `COPY FROM` PostgreSQL insertion?

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: Samuel Marks <samuelmarks(at)gmail(dot)com>
Cc: psycopg(at)lists(dot)postgresql(dot)org
Subject: Re: Escape Python to `json[]` for `COPY FROM` PostgreSQL insertion?
Date: 2023-02-21 09:57:05
Message-ID: CA+mi_8YZt4QjUY5YMTUVZGByRZ88f=X=TEoLuJG6ZQy3SiuW8w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On Tue, 21 Feb 2023 at 05:34, Samuel Marks <samuelmarks(at)gmail(dot)com> wrote:
>
> How do I insert into a table with a `json[]` column using the `COPY FROM` syntax?

you can just use psycopg 3 and pass it the records as tuples with the
objects you want to copy. The values of your json[] fields can be a
list of `Json` wrappers.

https://www.psycopg.org/psycopg3/docs/basic/copy.html#writing-data-row-by-row
https://www.psycopg.org/psycopg3/docs/basic/adapt.html#json-adaptation

If you want to know the escaping rules, they are in the Postgres documentation:

https://www.postgresql.org/docs/current/arrays.html#ARRAYS-INPUT

-- Daniele

In response to

Browse psycopg by date

  From Date Subject
Next Message Michael P. McDonnell 2023-04-23 20:45:56 Consecutive Inserts Freeze Execution of Psycopg3
Previous Message Aryeh Leib Taurog 2023-02-21 08:55:37 Re: Escape Python to `json[]` for `COPY FROM` PostgreSQL insertion?