Re: list all columns in db

From: Jon Sime <jsime(at)mediamatters(dot)org>
To: Jonathan Vanasco <postgres(at)2xlp(dot)com>
Cc: pgsql general <pgsql-general(at)postgresql(dot)org>
Subject: Re: list all columns in db
Date: 2007-06-07 22:36:07
Message-ID: 46688857.30003@mediamatters.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jonathan Vanasco wrote:
>
> Does anyone have a trick to list all columns in a db ?

No trickery, just exploit the availability of the SQL standard
information_schema views:

select table_schema, table_name, column_name
from information_schema.columns
where table_schema not in ('pg_catalog','information_schema')
order by 1,2,3

If you want an equivalent that uses pg_catalog (non-portable outside of
PostgreSQL) you could instead do:

select n.nspname as table_schema, c.relname as table_name,
a.attname as column_name
from pg_catalog.pg_attribute a
join pg_catalog.pg_class c on (a.attrelid = c.oid)
join pg_catalog.pg_namespace n on (c.relnamespace = n.oid)
where c.relkind in ('r','v') and a.attnum > 0
and n.nspname not in ('pg_catalog','information_schema')
order by 1,2,3

-Jon

--
Senior Systems Developer
Media Matters for America
http://mediamatters.org/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Coffin, Ronald 2007-06-07 22:44:31 Re: [ANNOUNCE] Advisory on possibly insecure security definer functions
Previous Message Rodrigo De León 2007-06-07 22:34:26 Re: list all columns in db