Por exemplo, como classificar a saída abaixo pelos valores em “Coluna” no psql
próprio shell?
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)
Eu sei que isso seria fácil de fazer fora do psql
shell, por exemplo no 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 |
mas estou curioso para psql
saber se isso poderia ser feito.
Não conheço uma maneira de fazer isso para os comandos de barra em si, mas eles usam os dados exatos que estão disponíveis no arquivo
information_schema
.Então, para \d você pode consultar
information_schema.columns
a tabela com sql típico e depois usarorder by
.Então, no seu caso, assim:
Você não pode alterar a ordem de classificação de um
psql
metacomando, mas pode descobrir a consulta de metadados por trás da saída:Isso também mostrará a consulta de metadados. Copie e cole essa consulta e adicione sua própria
ORDER BY
cláusula.