Open the table in PGAdmin and copy the SQL Create Table statement at the bottom of the SQL panel. Then open the Query Tool and paste. If there is data in the table, change the name of the table to "new_name", if not, delete the comment "-" in the line "Delete table". Edit the column sequence as required. Remember the missing / extra comma in the last column if you move it. Run the new SQL Create Table command. Refresh and ... voila.
For empty tables at the design stage, this method is quite practical.
If the table has data, we also need to change the sequence of data columns. It's simple: use INSERT to import the old table into the new version with:
INSERT INTO new ( c2, c3, c1 ) SELECT * from old;
... where c2 , c3 , c1 are the columns c1 , c2 , c3 old table in their new positions. Please note that in this case you must use the "new" name for the edited "old" table, otherwise you will lose your data . If the column names are many, long and / or complex, use the same method as above to copy the new table structure into a text editor and create a new list of columns there before copying it to the INSERT .
After making sure everything is in order, DROP old table and change the "new" name to "old" using ALTER TABLE new RENAME TO old; and you're done.
marcopolo Jan 11 '15 at 10:56 2015-01-11 10:56
source share