How do I alter the position of a column in a PostgreSQL database table?

前端 未结 9 1553
[愿得一人]
[愿得一人] 2020-11-30 00:25

I\'ve tried the following, but I was unsuccessful:

ALTER TABLE person ALTER COLUMN dob POSITION 37;
9条回答
  •  情歌与酒
    2020-11-30 01:00

    This post is old and probably solved but I had the same issue. I resolved it by creating a view of the original table specifying the new column order.

    From here I could either use the view or create a new table from the view.

        CREATE VIEW original_tab_vw AS
        SELECT a.col1, a.col3, a.col4, a.col2
        FROM original_tab a
        WHERE a.col1 IS NOT NULL --or whatever
    
        SELECT * INTO new_table FROM original_tab_vw
    

    Rename or drop the original table and set the name of the new table to the old table.

提交回复
热议问题