问题
I am trying to change the column order in the table of postgressql. But I didn't get any clue or answer. I think this functionality is add in new version. I am using postgres 11. Ex
No column
1 Id
2 Lastname
3 Firstname
Now i want to change Firstname on 2nd position.
回答1:
You would have to drop and re-create the table or at least the lastname column for that:
BEGIN;
ALTER TABLE atable RENAME lastname TO oldcol;
ALTER TABLE atable ADD lastname text NOT NULL;
UPDATE atable SET lastname = oldcol;
ALTER TABLE atable DROP oldcol;
COMMIT;
But the exercise is pretty pointless, since you can always determine the order in which you get the columns in the SELECT clause. You aren't using SELECT *, are you? That would be problematic for other reasons as well; it is only useful for ad-hoc queries.
来源:https://stackoverflow.com/questions/60145157/change-column-order-in-table-of-postgres