Postgres default sort by id - worldship Postgres default sort by id - worldship postgresql postgresql

Postgres default sort by id - worldship


Rows are returned in an unspecified order, per sql specs, unless you add an order by clause. In Postgres, that means you'll get rows in, basically, the order that live rows read on the disk.

If you want a consistent order without needing to add an order by clause, create a view as suggested in Jack's comment.


There is no such thing as a "default sort". Rows in a table are not sorted.

You could fake this with a view (as suggested by Jack Maney) there is no way you can influence the order of the rows that are returned.

But if you do that, be aware that adding an additional ORDER BY to a SELECT based on that view will sort the data twice.

Another option might be to run the CLUSTER command on that table to physically order the rows on the disk according to the column you want. But this sill does not guarantee that the rows are returned in that order. Not even with a plain SELECT * FROM your_table (but chances are reasonably high for that).You will need to re-run this statement on a regular basis because the order created by the CLUSTER command is not automatically maintained.


For what it's worth, which probably isn't much, PostgreSQL's "default" ordering is based on the time the records were last updated. The most recently updated records will appear last.