Thread: Getting list of columns in a table

Getting list of columns in a table

From
Mike Blackwell
Date:
Sorry if this is a FAQ, but didn't see it in the web site FAQ or in the
docs I have.

I need a SQL statement which will return a list of the column names for
a (possibly empty) table.  FWIW, I'm using Perl and DBI.

TiA,
Mike
mblackwell@wallace.com





Re: Getting list of columns in a table

From
Jason Earl
Date:
The trick is to fire up psql with -E flag.  That causes psql to spit
out the SQL it uses to query the system tables for things like:

\d tablename

I believe that the query you need is:

SELECT a.attname
FROM pg_class c, pg_attribute a
WHERE c.relname = 'schedule'
  AND a.attnum > 0 AND a.attrelid = c.oid
ORDER BY a.attnum;

Jason


Mike Blackwell <mblackwell@wallace.com> writes:

> Sorry if this is a FAQ, but didn't see it in the web site FAQ or in the
> docs I have.
>
> I need a SQL statement which will return a list of the column names for
> a (possibly empty) table.  FWIW, I'm using Perl and DBI.
>
> TiA,
> Mike
> mblackwell@wallace.com
>
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster