Skip to content

Instantly share code, notes, and snippets.

@righi
Created May 13, 2015 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save righi/2f35bad8ee5d97431963 to your computer and use it in GitHub Desktop.
Save righi/2f35bad8ee5d97431963 to your computer and use it in GitHub Desktop.
PostgreSQL : List all primary keys
SELECT a.attname AS pkey, c.relname as pkey_index_name, c2.relname AS table_name
FROM pg_index i
JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)
JOIN pg_catalog.pg_class c ON i.indexrelid = c.oid
JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE i.indisprimary
AND n.nspname = 'public';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment