Re: How to get the name of a table's primary key?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: chris(at)gammu(dot)ath(dot)cx
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to get the name of a table's primary key?
Date: 2002-03-16 20:58:35
Message-ID: 15377.1016312315@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Christian von Kietzell <chris(at)gammu(dot)ath(dot)cx> writes:
> Suppose, I've got the table shown above. I've only got its name. What
> I want is the column name the primary key is created on. How can I do
> that? Basically, which of foo_pkey and foo_data is the primary key?

You poke around in the system catalogs. Look in pg_index for a row that
describes an index on your table (join indrelid to pg_class.oid) and has
indisprimary true. (If no such row, there's no primary key.) Then look
in pg_attribute to get the column name(s) based on the column numbers
you see in indkey. See
http://www.ca.postgresql.org/users-lounge/docs/7.2/postgres/catalog-pg-index.html

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Samuel J. Sutjiono 2002-03-16 21:52:54 Variable Substitution for table name
Previous Message Christian von Kietzell 2002-03-16 19:46:12 Re: How to get the name of a table's primary key?