I'd like to do something like this:

INSERT INTO table2(name)
  INSERT INTO table1(name, address) VALUES ('me', 'home')

And then I would expect a new record in table2 with 'me' in name. This would work fine with a SELECT returning the values, but I'm having trouble with the nested insert.

1 Answer 1


Use a data modifying CTE:

with t1 (name) as (
  INSERT INTO table1 (name, address) VALUES ('me', 'home')
  returning name
insert into table2 (name)
select name 
from t1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.