例如,如何在 shell 本身中按“Column”中的值对下面的输出进行排序psql
?
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)
我知道这在 shell 之外很容易做到psql
,例如在 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 |
但我很好奇这是否可以完成psql
。
我不知道如何为斜杠命令本身执行此操作,但它们使用
information_schema
.因此,对于 \d 您可以
information_schema.columns
使用典型的 sql 查询表,然后使用order by
.所以在你的情况下是这样的:
您无法更改元命令的排序顺序
psql
,但您可以找出输出背后的元数据查询:这还将向您显示元数据查询。复制并粘贴该查询并添加您自己的
ORDER BY
子句。