Re: how to save primary key constraints

From: Joe Abbate <jma(at)freedomcircle(dot)com>
To: "J(dot)V(dot)" <jvsrvcs(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: how to save primary key constraints
Date: 2011-10-11 22:40:44
Message-ID: 4E94C5EC.4080607@freedomcircle.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10/11/2011 05:16 PM, J.V. wrote:
> I need to be able to query for all primary keys and save the table name
> and the name of the primary key field into some structure that I can
> iterate through later.
>
> How would I go about this? I want to hard code the number of tables and
> be able to iterate through some structure to get the table name and the
> primary key field.

A query such as the following may help:

SELECT nspname, conrelid::regclass::name, conname
FROM pg_constraint c
JOIN pg_namespace ON (connamespace = pg_namespace.oid)
LEFT JOIN pg_class on (conname = relname)
WHERE (nspname != 'pg_catalog' AND nspname != 'information_schema')
AND contype = 'p'
ORDER BY nspname, 2, conname;

The first column is the schema name, the second the table name and the
third the constraint (primary key) name.

Joe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sean Laurent 2011-10-11 22:41:10 Re: Postgres 9.01, Amazon EC2/EBS, XFS, JDBC and lost connections
Previous Message Sean Laurent 2011-10-11 22:38:26 Re: Postgres 9.01, Amazon EC2/EBS, XFS, JDBC and lost connections