Created
May 13, 2015 22:48
-
-
Save righi/2f35bad8ee5d97431963 to your computer and use it in GitHub Desktop.
PostgreSQL : List all primary keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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