public inbox for [email protected]
help / color / mirror / Atom feedFrom: Vianello, Dan A <[email protected]>
To: barry kimelman <[email protected]>
To: [email protected] <[email protected]>
Subject: RE: postgresql rookie needs help with system catalog
Date: Wed, 22 Jan 2020 15:05:29 +0000
Message-ID: <[email protected]> (raw)
In-Reply-To: <CAEOpmvoj8itCG-c9fOABreWZ1YjJL-Zp-Ern5=SzCxfihhiBkw@mail.gmail.com>
References: <CAEOpmvoj8itCG-c9fOABreWZ1YjJL-Zp-Ern5=SzCxfihhiBkw@mail.gmail.com>
If you run psql with the –E flag at start then it will echo the queries used to generate all of the \ commands.
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN
'foreign table' WHEN 'p' THEN 'table' WHEN 'I' THEN 'index' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p','v','m','S','f','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
From: barry kimelman [mailto:[email protected]]
Sent: Wednesday, January 22, 2020 8:52 AM
To: [email protected]
Subject: postgresql rookie needs help with system catalog
I am trying to come up with a query of the system catalog tables to give me the same info as the psql command "\d tablename"
so far I have come up with the following query
1 SELECT
2 a.attname as "Column",
3 pg_catalog.format_type(a.atttypid, a.atttypmod) as "Datatype"
4 FROM
5 pg_catalog.pg_attribute a
6 WHERE
7 a.attnum > 0
8 AND NOT a.attisdropped
9 AND a.attrelid = (
10 SELECT c.oid
11 FROM pg_catalog.pg_class c
12 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
13 WHERE c.relname = 'laptop'
14 AND pg_catalog.pg_table_is_visible(c.oid)
15 );
which yields the following result
Column | Datatype
---------------+------------------------
id | integer
name | character varying(250)
price | double precision
purchase_date | date
(4 rows)
But how do I get the Collation and Nullable and Default values displayed by the "psql \d" command ?
Thanks.
--
Barrry Kimelman
Winnipeg, Manitoba, Canada
E-MAIL CONFIDENTIALITY NOTICE:
The contents of this e-mail message and any attachments are intended solely for the addressee(s) and may contain confidential and/or legally privileged information. If you are not the intended recipient of this message or if this message has been addressed to you in error, please immediately alert the sender by reply e-mail and then delete this message and any attachments. If you are not the intended recipient, you are notified that any use, dissemination, distribution, copying, or storage of this message or any attachment is strictly prohibited.
view thread (4+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Subject: RE: postgresql rookie needs help with system catalog
In-Reply-To: <[email protected]>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox