Re: Retrieve the primary key of a table

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alejandro <apinoo(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Retrieve the primary key of a table
Date: 2009-08-03 14:14:36
Message-ID: 16268.1249308876@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Alejandro <apinoo(at)gmail(dot)com> writes:
> Hi. I need to retrieve the primary key of a table and their value.
> I tried with this
> http://wiki.postgresql.org/wiki/Retrieve_primary_key_columns:

> SELECT
> pg_attribute.attname,
> format_type(pg_attribute.atttypid, pg_attribute.atttypmod)
> FROM pg_index, pg_class, pg_attribute
> WHERE
> pg_class.oid = 'TABLENAME'::regclass AND
> indrelid = pg_class.oid AND
> pg_attribute.attrelid = pg_class.oid AND
> pg_attribute.attnum = any(pg_index.indkey);

> But this shows indexes too.

Hm, that query does not do what the page claims. You need to add
"AND indisprimary".

If you want something more standardized, there are information_schema
views that would help with this problem, too.

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Rafa García Castillo 2009-08-03 16:09:55 Where can I find the PostgreSQL 8.2.5 binaries?
Previous Message Michael Wood 2009-08-03 12:58:13 Re: Retrieve the primary key of a table