I know that \d table_name lists all columns for that table, but is there a way to list them sorted in alphabetical order?
Asked
Active
Viewed 2.5k times
14
oxfist
- 361
- 1
- 5
- 14
2 Answers
30
Generally, use the information_schema:
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'blah'
ORDER BY column_name ASC;
Craig Ringer
- 57,821
- 6
- 162
- 193
0
This also works:
SELECT col_attr.attname as "ColumnName"
FROM pg_catalog.pg_attribute col_attr
WHERE col_attr.attnum > 0
AND NOT col_attr.attisdropped
AND col_attr.attrelid =
(SELECT cls.oid
FROM pg_catalog.pg_class cls
LEFT JOIN pg_catalog.pg_namespace ns
ON ns.oid = cls.relnamespace
WHERE cls.relname = 'MyTable')
ORDER BY "ColumnName" ASC
Mark Storey-Smith
- 31,860
- 9
- 90
- 125
Parthi P
- 1