Unlike most SQL derivatives, PostgreSQL doesn’t use the traditional DESCRIBE TABLE foo
syntax; it uses d foo
(from the psql
interpreter).
That’s great, except that (especially when I have psql
open in a terminal with limited width) it can be difficult to read the output of d
, because there are a bunch of columns and a bunch of values, and they can be hard to line everything up.
So, my simple question is, when using d
and its variants (eg. df
), is there any way to only get one column of output back? For instance, if I do d users
, instead of getting:
Table "public.users"
Column | Type | Collation | Nullable | Default
------------+--------------------------+-----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
created_at | timestamp with time zone | | not null | CURRENT_TIMESTAMP
updated_at | timestamp with time zone | | not null | CURRENT_TIMESTAMP
username | character varying(255) | | not null |
title | character varying(500) | | |
first_name | character varying(50) | | |
last_name | character varying(50) | | |
is there some way (eg. d users Column
, although this doesn’t work) to get just:
Table "public.users"
Column |
------------+
id |
created_at |
updated_at |
username |
first_name |
last_name |
4
Answers
You should check the command
gdesc
it describes the result columns that a query will have.
So let’s assume you have a simple table with three columns. It would help if you used it like below
and the result that you will receive would be
I also encourage you to check this thread
Actual subset of
d
outputYes. Pop the hood by setting
ECHO-HIDDEN
toon
and throw away anything you don’t like, grabbing only what you want. Every individual piece of what that output is constructed from, will work fine on its own.PostgreSQL doesn’t understand
d
. It’s an internal meta-command ofpsql
, which is a client like any other. The data you see is a result of the client issuing a series of queries and gluing their output together – the setting above lets you learn what those queries were. You can use them "by hand" or even rearrange them to better fit your purpose.Terminal width
You might want to check out
expanded
mode.psql
offers multiple output formats to select from:wrapped
has adjustablecolumns
width. You should also have control over your terminal screen buffer layout in its properties – there, you could crank up the width and whenever something’s too wide, you’ll scroll right if you want or just leave it sticking out, off-screen.Querying structures/schema
There’s SQL-standard compliant
information_schema
namespace, and in it, there’scolumns
view. You can go there directly:The views in that schema point to the same place
psql
gets the list of columns for tables and views when you rund
. The actual data is in the system catalog whichpsql
queries directly.Use the system catalog pg_attribute directly:
pg_attribute
stores the column names for table like objects e.g. tables, indexes, views, etc. The'users'::regclass
translates the table name intooid
needed forattrelid
.attnum > 0
eliminates the system columns,cmax
,cmin
,ctid
, etc.You can use
less -S
orleafpad
or any other text editor program, that would not wrap lines, as a pager:Or you can query information schema to get similar data with extended output: