Re: Retrieve the primary key of a table

From: Lacey Powers <lacey(dot)powers(at)commandprompt(dot)com>
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 06:21:39
Message-ID: 4A7681F3.4060703@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Jasen Betts 2009-08-03 10:46:15 Re: Weekends between
Previous Message Alejandro 2009-08-03 02:24:44 Retrieve the primary key of a table