Copy a row column from one table to another in ON CONFLICT

Viewed 274

Using Postgres 9.6, I am copying a row(selected columns) from one table to another. I am running:

INSERT INTO copy_on_write(id, images)
    SELECT id, images
    FROM listings
    WHERE id = 1 AND posted_by = foo
ON CONFLICT (id) DO UPDATE SET images = listings.images 

With the conflict because id already exists, I am getting missing FROM-clause entry for table "listings".

So, I tried:

INSERT INTO copy_on_write(id, images)
    SELECT id, images
    FROM listings
    WHERE id = 1 AND posted_by = foo
ON CONFLICT (id) DO UPDATE SET images = images FROM listings

but then I get syntax error at or near "FROM"

How do I handle this ON CONFLICT (id) so it updates the images column if the row id already exists?

1 Answers
Related