News for hackers. Stuff that matters.

Reordering Columns in PostgreSQL

This is kind of a stupid question but I see lot of people here recommend postgresql so I decided to give it a try.

I stumbled upon the fact that it's not possible to rearrange columns ordering.

Is this not too much of a constraint ? I mean what's the workaround for eg for the analysts using it daily ? Columns just pile at the end of every table without any consistency ?

Sorry for the broken english (french)

( 8 comments | on HN )
Re:Reordering Columns in PostgreSQL by rangerelf on Thursday January 05, 2023 @10:25AM (#34260934)
The order of the columns in your table creation statements is irrelevant to the order of how the columns return to you in your queries.

Your select statement specified the order of your result columns in your result set. Your visualization tools should have the option to reorder the displayed columns to your liking or need.

Re:Reordering Columns in PostgreSQL by HNBRN on Thursday January 05, 2023 @10:41AM (#34261242)
Thanks. I checked and dbeaver can indeed store columns ordering as a filtered view. Seems like a bit overcomplicated to me but I guess I've been spoiled with mysql in which the ordering is trivial.
Re:Reordering Columns in PostgreSQL by sargstuff on Friday January 06, 2023 @06:19PM (#34282784)
visual example : https://www.w3resource.com/sql-exercises/sql-retrieve-exerci...

verbal examples : discussion on various ways to reorder columns : https://stackoverflow.com/questions/1605144/how-to-change-co...

Re:Reordering Columns in PostgreSQL by wcorrales on Thursday January 05, 2023 @12:08PM (#34262937)
Re:Reordering Columns in PostgreSQL by sargstuff on Thursday January 05, 2023 @04:15PM (#34266703)
Reviewing database normal forms might be helpful/useful. [1]

Non-postgress, old school method:

1) Dump table data to ascii delimited file.

2) Use awk/gawk to re-arrange columns in ascii delimited file.

3) drop old data table.

4) create new data table to upload the awk/gawk data generated in #2.

5) upload awk/gawk data from #2.

--------

[1] : https://www.geeksforgeeks.org/first-normal-form-1nf/?ref=lbp

Re:Reordering Columns in PostgreSQL by HNBRN on Friday January 06, 2023 @03:16AM (#34272263)
Thanks, we could definitely start from this
Re:Reordering Columns in PostgreSQL by sargstuff on Sunday January 15, 2023 @03:06PM (#34392772)
Re:Reordering Columns in PostgreSQL by sargstuff on Friday January 06, 2023 @06:25PM (#34282851)