In Postgres (version 9.4 fwiw), an INSERT INTO ... SELECT query seems to insert based on column position rather than name:
pg=# CREATE TEMPORARY TABLE x (a int, b int);
CREATE TABLE
pg=# INSERT INTO x (a, b) SELECT 1 as b, 2 as a;
INSERT 0 1
pg=# SELECT * FROM x;
a | b
---+---
1 | 2
(1 row)
Is there any way to structure this query or something similar such that the columns would be inserted by name rather than positionally? I would want the result here to be:
pg=# SELECT * FROM x;
a | b
---+---
2 | 1
(1 row)
The context here is that the contents of the SELECT are being defined in a different place than the overall INSERT call, so it's difficult to enforce a common order.