Re: Retrieve the primary key of a table - Mailing list pgsql-novice

From Tom Lane
Subject Re: Retrieve the primary key of a table
Date
Msg-id 16268.1249308876@sss.pgh.pa.us
Whole thread Raw
In response to Retrieve the primary key of a table  (Alejandro <apinoo@gmail.com>)
Responses Re: Retrieve the primary key of a table  (Alejandro <apinoo@gmail.com>)
List pgsql-novice
Alejandro <apinoo@gmail.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

pgsql-novice by date:

Previous
From: Michael Wood
Date:
Subject: Re: Retrieve the primary key of a table
Next
From: Roger D Vargas
Date:
Subject: converting an old databaes to postgresql 8.1