Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A How to sort the output of meta / slash commands in `psql`?

It's been a while, but the answer is "yes and no," I believe. No: The describe-schema command doesn't (last I heard) allow any modification. Yes: The describe-schema command also doesn't do any...

posted 1mo ago by John C‭  ·  edited 1mo ago by toraritte‭

Answer
#2: Post edited by user avatar toraritte‭ · 2024-05-12T23:39:38Z (about 1 month ago)
Hope you don't mind me adding this to your answer. I thought there would be no point in opening another one. Thanks again!
  • It's been a while, but the answer is "yes and no," I believe.
  • No: The describe-schema command doesn't (last I heard) allow any modification.
  • Yes: The describe-schema command *also* doesn't do anything that a `SELECT` statement from `information_schema` doesn't do, so something like this should work.
  • ```sql
  • SELECT
  • column_name
  • FROM
  • information_schema.columns
  • WHERE
  • table_schema = 'public'
  • AND
  • table_name = 'auth_user'
  • ORDER BY
  • column_name ASC;
  • ```
  • Assuming that they the PostgreSQL team hasn't moved anything in the years that I've had to deal with everything else, that should at least get you close.
  • It's been a while, but the answer is "yes and no," I believe.
  • No: The describe-schema command doesn't (last I heard) allow any modification.
  • Yes: The describe-schema command *also* doesn't do anything that a `SELECT` statement from `information_schema` doesn't do, so something like this should work.
  • ```sql
  • SELECT
  • column_name
  • FROM
  • information_schema.columns
  • WHERE
  • table_schema = 'public'
  • AND
  • table_name = 'auth_user'
  • ORDER BY
  • column_name ASC;
  • ```
  • Assuming that they the PostgreSQL team hasn't moved anything in the years that I've had to deal with everything else, that should at least get you close.
  • ---
  • Another approach to get around the immutability of the [`psql`](https://www.postgresql.org/docs/current/app-psql.htm) meta-commands is to list the SQL queries that are executed by them, and modify the relevant parts (e.g., by adding an `ORDER BY` clause).
  • For example, here's how to list the queries behind `\d`:
  • ```
  • $ psql -E -d my_db
  • my_db=> \d auth_user
  • ```
  • or
  • ```
  • my_db=> \set ECHO_HIDDEN on
  • my_db=> \d auth_user
  • ```
  • That will also show you the metadata query. Copy and paste that query and add your own `ORDER BY` clause.
#1: Initial revision by user avatar John C‭ · 2024-05-12T21:33:56Z (about 1 month ago)
It's been a while, but the answer is "yes and no," I believe.

No:  The describe-schema command doesn't (last I heard) allow any modification.

Yes:  The describe-schema command *also* doesn't do anything that a `SELECT` statement from `information_schema` doesn't do, so something like this should work.

```sql
SELECT
  column_name
FROM
  information_schema.columns 
WHERE
  table_schema = 'public' 
AND
  table_name = 'auth_user' 
ORDER BY
  column_name ASC;
```

Assuming that they the PostgreSQL team hasn't moved anything in the years that I've had to deal with everything else, that should at least get you close.