Re: Finding PrimaryKey columns of a table

From: rmcm(at)compsoft(dot)com(dot)au
To: swalker <swalker(at)iglou(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Finding PrimaryKey columns of a table
Date: 2000-05-02 21:46:49
Message-ID: 14607.19657.510234.273451@fellini.mcmaster.wattle.id.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

The following may help, substitiuting "tablename";

select a.attname
from pg_index i, pg_attribute a
where i.indrelid =
(select oid as class_oid from pg_class where relname = 'tablename')
and i.indkey[0] = a.attnum
and a.attrelid = i.indrelid
and i.indisprimary = 't';

swalker writes:
> Is there a query I can run against the pg system tables to figure out what
> columns of the table make up the primary key?
>
> I've messed around w/ psql -E (which is very cool btw) and can't seem to
> find the exact query I'm looking for. I can get all indexes for a table,
> but I can't seem to limit the result set to only the primary key.
>
> Am I missing something obvious ?
>
> Thanks
> Steve
>
>

--
Rex McMaster rex(at)mcmaster(dot)wattle(dot)id(dot)au
http://www.compsoft.com.au/~rmcm/pgp-pk

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jack Zhu 2000-05-02 21:53:41 JDBC problem on Postgresql 7 beta 5
Previous Message Peter Eisentraut 2000-05-02 21:29:16 Re: Enumerated data type