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`?

For example, how to sort the output below by the values in "Column" in the psql shell itself? my_db=> \d auth_user Table "public.auth_user" Column ...

1 answer  ·  posted 6mo ago by toraritte‭  ·  edited 6mo ago by MER‭

#2: Post edited by user avatar MER‭ · 2024-05-18T05:33:46Z (6 months ago)
Just adding a couple more tags (psql looks and sounds a lot like plsql ... better to add more tags to further differentiate)
How to sort the output of meta / slash commands in `psql`?
For example, how to sort the output below by the values in "Column" in the `psql` shell itself?

```
my_db=> \d auth_user
                                        Table "public.auth_user"
    Column    |           Type           | Collation | Nullable |                Default                
--------------+--------------------------+-----------+----------+---------------------------------------
 id           | integer                  |           | not null | nextval('auth_user_id_seq'::regclass)
 is_superuser | boolean                  |           | not null | 
 username     | character varying(150)   |           | not null | 
 first_name   | character varying(150)   |           |          | 
 last_name    | character varying(150)   |           | not null | 
 email        | character varying(254)   |           |          | 
 is_staff     | boolean                  |           | not null | false
 is_active    | boolean                  |           | not null | false
 password     | character varying(128)   |           |          | 
 last_login   | timestamp with time zone |           |          | 
 date_joined  | timestamp with time zone |           |          | 
Indexes:
    "auth_user_pkey" PRIMARY KEY, btree (id)
    "auth_user_username_6821ab7c_like" btree (username varchar_pattern_ops)
    "auth_user_username_key" UNIQUE CONSTRAINT, btree (username)
```

I know that this would be easy to do outside the `psql` shell, for example in Bash,

```
$ psql -U postgres -h localhost -d my_db -c '\d auth_user' | grep '^.*|' | sort -t'|' -k1

    Column    |           Type           | Collation | Nullable |                Default                
 date_joined  | timestamp with time zone |           |          | 
 email        | character varying(254)   |           |          | 
 first_name   | character varying(150)   |           |          | 
 id           | integer                  |           | not null | nextval('auth_user_id_seq'::regclass)
 is_active    | boolean                  |           | not null | false
 is_staff     | boolean                  |           | not null | false
 is_superuser | boolean                  |           | not null | 
 last_login   | timestamp with time zone |           |          | 
 last_name    | character varying(150)   |           | not null | 
 password     | character varying(128)   |           |          | 
 username     | character varying(150)   |           | not null | 
```

but I'm curious if this could be done in `psql` at all.
#1: Initial revision by user avatar toraritte‭ · 2024-05-12T17:37:05Z (6 months ago)
How to sort the output of meta / slash commands in `psql`?
For example, how to sort the output below by the values in "Column" in the `psql` shell itself?

```
my_db=> \d auth_user
                                        Table "public.auth_user"
    Column    |           Type           | Collation | Nullable |                Default                
--------------+--------------------------+-----------+----------+---------------------------------------
 id           | integer                  |           | not null | nextval('auth_user_id_seq'::regclass)
 is_superuser | boolean                  |           | not null | 
 username     | character varying(150)   |           | not null | 
 first_name   | character varying(150)   |           |          | 
 last_name    | character varying(150)   |           | not null | 
 email        | character varying(254)   |           |          | 
 is_staff     | boolean                  |           | not null | false
 is_active    | boolean                  |           | not null | false
 password     | character varying(128)   |           |          | 
 last_login   | timestamp with time zone |           |          | 
 date_joined  | timestamp with time zone |           |          | 
Indexes:
    "auth_user_pkey" PRIMARY KEY, btree (id)
    "auth_user_username_6821ab7c_like" btree (username varchar_pattern_ops)
    "auth_user_username_key" UNIQUE CONSTRAINT, btree (username)
```

I know that this would be easy to do outside the `psql` shell, for example in Bash,

```
$ psql -U postgres -h localhost -d my_db -c '\d auth_user' | grep '^.*|' | sort -t'|' -k1

    Column    |           Type           | Collation | Nullable |                Default                
 date_joined  | timestamp with time zone |           |          | 
 email        | character varying(254)   |           |          | 
 first_name   | character varying(150)   |           |          | 
 id           | integer                  |           | not null | nextval('auth_user_id_seq'::regclass)
 is_active    | boolean                  |           | not null | false
 is_staff     | boolean                  |           | not null | false
 is_superuser | boolean                  |           | not null | 
 last_login   | timestamp with time zone |           |          | 
 last_name    | character varying(150)   |           | not null | 
 password     | character varying(128)   |           |          | 
 username     | character varying(150)   |           | not null | 
```

but I'm curious if this could be done in `psql` at all.