Re: [SQL] How do I get column names? - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] How do I get column names?
Date
Msg-id 12856.945125447@sss.pgh.pa.us
Whole thread Raw
In response to How do I get column names?  ("Steven M. Wheeler" <swheeler@sabre.com>)
Responses System Attribute
List pgsql-sql
"Steven M. Wheeler" <swheeler@sabre.com> writes:
> What is the best way to query the DB, to get the column names for a
> particular table?

Usually you'd join across pg_attribute and pg_class, assuming that you
were starting from a table name.  For example:

regression=> select attname, attnum from pg_attribute, pg_class where
regression-> attrelid = pg_class.oid and relname = 'int8_tbl';
attname|attnum
-------+------
cmax   |    -6
xmax   |    -5
cmin   |    -4
xmin   |    -3
oid    |    -2
ctid   |    -1
q1     |     1
q2     |     2
(8 rows)

You probably would also want 'and attnum > 0' in the where-clause to
exclude the system attributes...
        regards, tom lane


pgsql-sql by date:

Previous
From: "Steven M. Wheeler"
Date:
Subject: How do I get column names?
Next
From: Drew Whittle
Date:
Subject: System Attribute