PostgreSQL
 SQL >> Database >  >> RDS >> PostgreSQL

Importare un file JSON in PostgreSQL

Puoi caricare il file JSON in una tabella temporanea e poi inserire i record nella tabella finale usando json_populate_recordset.

create unlogged table customer_import (doc json);
\copy customer_import from 'customers.json'

insert into customer (id, name, comment)
select p.*
from customer_import l
cross join lateral json_populate_recordset(null::customer, doc) as p
on conflict (id) do update
set name = excluded.name,
    comment = excluded.comment;

La funzione json_populate_recordset trasforma gli oggetti JSON in righe relazionali.