CREATE TABLE contacts(
id INT NOT NULL PRIMARY KEY,
name VARCHAR(50),
age INT NOT NULL,
active VARCHAR(10)
);
WITH contacts_json (doc) AS (VALUES(
'[
{
"id": 1,
"name": "Jimmy",
"age": 25,
"active": "yes"
},
{
"id": 2,
"name": "Annie",
"age": 22,
"active": "yes"
},
{
"id": 3,
"name": "John",
"age": 21,
"active": "no"
},
{
"id": 4,
"name": "Mark",
"age": 15,
"active": "no"
},
{
"id": 5,
"name": "Shelly",
"age": 20,
"active": "yes"
}
]'::json))
INSERT INTO contacts (id, name, age, active)
SELECT p.* FROM contacts_json l CROSS JOIN lateral
json_populate_recordset(NULL::contacts, doc) AS p ON conflict (id)
do UPDATE SET name = excluded.name, active = excluded.active;
select * from contacts as c
Comments