Re: How to get the name of a table's primary key? - Mailing list pgsql-general

From Tom Lane
Subject Re: How to get the name of a table's primary key?
Date
Msg-id 15377.1016312315@sss.pgh.pa.us
Whole thread Raw
In response to Re: How to get the name of a table's primary key?  (Christian von Kietzell <chris@gammu.ath.cx>)
List pgsql-general
Christian von Kietzell <chris@gammu.ath.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

pgsql-general by date:

Previous
From: Christian von Kietzell
Date:
Subject: Re: How to get the name of a table's primary key?
Next
From: "Samuel J. Sutjiono"
Date:
Subject: Variable Substitution for table name