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

From Lacey Powers
Subject Re: Retrieve the primary key of a table
Date
Msg-id 4A7681F3.4060703@commandprompt.com
Whole thread Raw
In response to Retrieve the primary key of a table  (Alejandro <apinoo@gmail.com>)
List pgsql-novice
Hello Alejandro,

Hm. Looks like you're very close. =)

Checking pg_catalog.pg_index, there's a column that should track the
data you're looking for.

indisprimary   | boolean    | not null

So, if you add another criteria to your WHERE clause,
"pg_index.indisprimary IS TRUE" that should return only the primary
keys. =)

Lacey
> 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.
> http://wiki.postgresql.org/wiki/Talk:Retrieve_primary_key_columns
>
> How can I get only the primary key?
>
> I have postgres 8.1.11.
>
> Thanks, Alejandro.


--
Lacey Powers

The PostgreSQL Company - Command Prompt, Inc. 1.503.667.4564 ext 104
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


pgsql-novice by date:

Previous
From: Frank Bax
Date:
Subject: Re: converting an old databaes to postgresql 8.1
Next
From: Michel Albert
Date:
Subject: Index size