I'm wanting the same. Yes, order isn't essential for my use-case, but it just rubs me the wrong way :)
What I'm doing to resolve it is as follows.
This method will ensure you KEEP any existing data,
- Create a new version of the table using the ordering I want, using a temporary name.
- Insert all data into that new table from the existing one.
- Drop the old table.
- Rename the new table to the "proper name" from "temporary name".
- Re-add any indexes you previously had.
- Reset ID sequence for primary key increments.
Current table order:
id, name, email
1. Create a new version of the table using the ordering I want, using a temporary name.
In this example, I want email
to be before name
.
CREATE TABLE mytable_tmp
(
id SERIAL PRIMARY KEY,
email text,
name text
);
2. Insert all data into that new table from the existing one.
INSERT INTO mytable_tmp --- << new tmp table
(
id
, email
, name
)
SELECT
id
, email
, name
FROM mytable; --- << this is the existing table
3. Drop the old table.
DROP TABLE mytable;
4. Rename the new table to the "proper name" from "temporary name".
ALTER TABLE mytable_tmp RENAME TO mytable;
5. Re-add any indexes you previously had.
CREATE INDEX ...
6. Reset ID sequence for primary key increments.
SELECT setval('public.mytable_id_seq', max(id)) FROM mytable;