{"id":5444,"date":"2016-10-24T04:21:17","date_gmt":"2016-10-24T04:21:17","guid":{"rendered":"http:\/\/www.withdata.com\/blog\/?p=5444"},"modified":"2017-04-20T11:14:14","modified_gmt":"2017-04-20T11:14:14","slug":"how-to-get-primary-key-of-a-table-from-postgresql","status":"publish","type":"post","link":"https:\/\/www.withdata.com\/blog\/postgresql\/how-to-get-primary-key-of-a-table-from-postgresql.html","title":{"rendered":"How to get primary key of a table from PostgreSQL"},"content":{"rendered":"<p><code>SELECT<br \/>\npg_attribute.attname,<br \/>\nformat_type(pg_attribute.atttypid, pg_attribute.atttypmod)<br \/>\nFROM pg_index, pg_class, pg_attribute, pg_namespace<br \/>\nWHERE<br \/>\npg_class.oid = 'my_table_name'::regclass AND<br \/>\nindrelid = pg_class.oid AND<br \/>\nnspname = 'my_schema_name' AND<br \/>\npg_class.relnamespace = pg_namespace.oid AND<br \/>\npg_attribute.attrelid = pg_class.oid AND<br \/>\npg_attribute.attnum = any(pg_index.indkey)<br \/>\nAND indisprimary<\/code><\/p>\n<p>See official version: <a href=\"http:\/\/wiki.postgresql.org\/wiki\/Retrieve_primary_key_columns\">http:\/\/wiki.postgresql.org\/wiki\/Retrieve_primary_key_columns<\/a><\/p>\n<p>See also:<\/p>\n<p><a href=\"http:\/\/www.withdata.com\/blog\/sqlite\/how-to-get-primary-key-of-a-table-from-sqlite.html\">How to get primary key of a table from SQLite<\/a><\/p>\n<p><a href=\"http:\/\/www.withdata.com\/blog\/mysql\/how-to-get-primary-key-of-a-table-from-mysql.html\">How to get primary key of a table from MySQL<\/a><\/p>\n<p><a href=\"http:\/\/www.withdata.com\/blog\/sql-server\/how-to-get-primary-key-of-a-table-from-sql-server.html\">How to get primary key of a table from SQL Server<\/a><\/p>\n<p><a href=\"http:\/\/www.withdata.com\/blog\/db2\/how-to-get-primary-key-of-a-table-from-db2.html\">How to get primary key of a table from DB2<\/a><\/p>\n<p><a href=\"http:\/\/www.withdata.com\/blog\/oracle\/how-to-get-primary-key-of-a-table-from-oracle.html\">How to get primary key of a table from Oracle<\/a><\/p>\n<p>Some PostgreSQL tools you can try: <a href=\"https:\/\/www.withdata.com\/postgresql\/\"> https:\/\/www.withdata.com\/postgresql\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SELECT pg_attribute.attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) FROM pg_index, pg_class, pg_attribute, pg_namespace WHERE pg_class.oid = &#8216;my_table_name&#8217;::regclass AND indrelid = pg_class.oid AND nspname = &#8216;my_schema_name&#8217; AND pg_class.relnamespace = pg_namespace.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = any(pg_index.indkey) AND indisprimary See official version: http:\/\/wiki.postgresql.org\/wiki\/Retrieve_primary_key_columns See also: How to get primary key of a table from SQLite How to get primary [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[430],"tags":[1719,1370],"class_list":["post-5444","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-get-primary-key","tag-postgresql"],"_links":{"self":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/5444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/comments?post=5444"}],"version-history":[{"count":3,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/5444\/revisions"}],"predecessor-version":[{"id":6472,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/5444\/revisions\/6472"}],"wp:attachment":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/media?parent=5444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/categories?post=5444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/tags?post=5444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}