I have a 2 tables with the following structures:
Table_1: id name age
Table_2: id table_1_id city state
I need to perform bulk insert into Table 1:
INSERT INTO Table_1(name, age)
VALUES ('A', 12), ('B', 13), ('C', 14)
RETURNING id
The ids returned from the first insert are to be used in the below query:
INSERT INTO Table_2(table_1_id, city, state)
VALUES (first_row_id, 'Austin', 'Texas'),
(second_row_id, 'Dallas', 'Texas'),
(third_row_id, 'Houston', 'Texas')
How can I achieve the same using CTE or any other efficient way with least amount of code in Postgres?