{"id":125783,"date":"2021-12-14T20:08:46","date_gmt":"2021-12-14T12:08:46","guid":{"rendered":"http:\/\/www.idc.net\/help\/125783\/"},"modified":"2021-12-14T20:08:46","modified_gmt":"2021-12-14T12:08:46","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8postgresql%e4%b8%ad%e6%9f%a5%e7%9c%8b%e6%95%b0%e6%8d%ae%e5%ba%93%e4%bf%a1%e6%81%af-postgresql%e6%9f%a5%e7%9c%8b%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/125783\/","title":{"rendered":"\u5982\u4f55\u5728PostgreSQL\u4e2d\u67e5\u770b\u6570\u636e\u5e93\u4fe1\u606f (postgresql\u67e5\u770b\u6570\u636e\u5e93)"},"content":{"rendered":"<p>PostgreSQL\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5f00\u6e90\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u4f01\u4e1a\u548c\u5f00\u53d1\u8005\u7684\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u4f5c\u4e2d\u3002\u5f53\u60a8\u5728\u4f7f\u7528PostgreSQL\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u67e5\u770b\u6570\u636e\u5e93\u4fe1\u606f\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u4e86\u89e3\u60a8\u7684\u6570\u636e\u5e93\u4ee5\u53ca\u5982\u4f55\u4f18\u5316\u5b83\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5728PostgreSQL\u4e2d\u67e5\u770b\u6570\u636e\u5e93\u4fe1\u606f\u7684\u5e38\u89c1\u65b9\u6cd5\u3002<\/p>\n<p>1. \u4f7f\u7528psql\u547d\u4ee4\u884c\u5de5\u5177<\/p>\n<p>psql\u662fPostgreSQL\u63d0\u4f9b\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u8ba9\u60a8\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u6267\u884cSQL\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fde\u63a5\u5230PostgreSQL\u6570\u636e\u5e93\uff1a<\/p>\n<p>\u201c`<\/p>\n<p>psql -d dbname -U username -h host -p port<\/p>\n<p>\u201c`<\/p>\n<p>\u5176\u4e2d\uff0cdbname\u662f\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0cusername\u662f\u6570\u636e\u5e93\u7528\u6237\u7684\u540d\u79f0\uff0chost\u662f\u6570\u636e\u5e93\u4e3b\u673a\u7684IP\u5730\u5740\u6216\u57df\u540d\uff0cport\u662f\u6570\u636e\u5e93\u76d1\u542c\u7684\u7aef\u53e3\u3002\u5982\u679c\u60a8\u6b63\u5728\u8fd0\u884c\u6570\u636e\u5e93\u670d\u52a1\u5668\u672c\u5730\u4e3b\u673a\u4e0a\u7684\u5b9e\u4f8b\uff0c\u5219\u53ef\u4ee5\u7701\u7565-h\u548c-p\u9009\u9879\u3002<\/p>\n<p>\u4e00\u65e6\u60a8\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0bSQL\u8bed\u53e5\u67e5\u770b\u6570\u636e\u5e93\u4fe1\u606f\uff1a<\/p>\n<p>\u201c`<\/p>\n<p># \u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u7684\u540d\u79f0<\/p>\n<p>SELECT current_database();<\/p>\n<p># \u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u7684\u7248\u672c\u4fe1\u606f<\/p>\n<p>SELECT version();<\/p>\n<p># \u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u7684\u7f16\u7801\u65b9\u5f0f<\/p>\n<p>SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = \u2018dbname\u2019;<\/p>\n<p># \u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u7684\u5927\u5c0f<\/p>\n<p>SELECT pg_size_pretty(pg_database_size(\u2018dbname\u2019));<\/p>\n<p># \u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u6240\u6709\u7684\u8868\u540d<\/p>\n<p>SELECT tablename FROM pg_tables WHERE schemaname = \u2018public\u2019;<\/p>\n<p># \u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u6240\u6709\u7684\u7d22\u5f15\u540d<\/p>\n<p>SELECT indexname FROM pg_indexes WHERE schemaname = \u2018public\u2019;<\/p>\n<p>\u201c`<\/p>\n<p>2. \u4f7f\u7528pgAdmin<\/p>\n<p>pgAdmin\u662f\u4e00\u4e2a\u6d41\u884c\u7684PostgreSQL\u7ba1\u7406\u5de5\u5177\uff0c\u63d0\u4f9b\u56fe\u5f62\u5316\u754c\u9762\u548c\u8bb8\u591a\u6709\u7528\u7684\u529f\u80fd\uff0c\u5982\u6570\u636e\u5e93\u8fde\u63a5\u3001\u67e5\u8be2\u6267\u884c\u548c\u6570\u636e\u5e93\u7ba1\u7406\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u5728pgAdmin\u4e2d\u67e5\u770b\u6570\u636e\u5e93\u4fe1\u606f\uff1a<\/p>\n<p>\u2013 \u6253\u5f00pgAdmin\u5e76\u8fde\u63a5\u5230PostgreSQL\u6570\u636e\u5e93<\/p>\n<p>\u2013 \u9009\u62e9\u8981\u67e5\u770b\u7684\u6570\u636e\u5e93<\/p>\n<p>\u2013 \u5728\u201c\u5de5\u5177\u680f\u201d\u4e2d\u9009\u62e9\u201c\u5de5\u5177&gt;\u7edf\u8ba1\u4fe1\u606f\u2026\u201d<\/p>\n<p>\u2013 \u5728\u201c\u7edf\u8ba1\u4fe1\u606f\u201d\u5bf9\u8bdd\u6846\u4e2d\uff0c\u53ef\u4ee5\u67e5\u770b\u6709\u5173\u6570\u636e\u5e93\u3001\u8868\u3001\u7d22\u5f15\u548c\u51fd\u6570\u7b49\u7684\u5404\u79cd\u7edf\u8ba1\u4fe1\u606f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u201c\u5bf9\u8c61\u201d\u9009\u9879\u5361\u4e0a\u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u4e2d\u7684\u6240\u6709\u8868\uff0c\u5e76\u68c0\u67e5\u5b83\u4eec\u7684\u884c\u8ba1\u6570\u3001\u7a7a\u95f4\u5360\u7528\u3001\u7d22\u5f15\u4f7f\u7528\u60c5\u51b5\u548c\u9010\u6e10\u589e\u52a0\u7684\u884c\u8ba1\u6570\u3002\u8fd8\u53ef\u4ee5\u5728\u201c\u51fd\u6570\u201d\u9009\u9879\u5361\u4e0a\u67e5\u770b\u6570\u636e\u5e93\u4e2d\u7684\u6240\u6709\u5b58\u50a8\u8fc7\u7a0b\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5e76\u68c0\u67e5\u5b83\u4eec\u7684\u8c03\u7528\u8ba1\u6570\u548c\u6267\u884c\u65f6\u95f4\u5206\u5e03\u7b49\u3002<\/p>\n<p>3. \u4f7f\u7528SQL\u67e5\u8be2<\/p>\n<p>\u9664\u4e86\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u548cpgAdmin\u4e4b\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528SQL\u8bed\u53e5\u76f4\u63a5\u67e5\u8be2PostgreSQL\u6570\u636e\u5e93\u4ee5\u67e5\u770b\u6709\u5173\u6570\u636e\u5e93\u7684\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u67e5\u8be2\uff1a<\/p>\n<p>\u201c`<\/p>\n<p>\u2014 \u663e\u793a\u5f53\u524d\u6240\u6709\u8fde\u63a5\u5230PostgreSQL\u6570\u636e\u5e93\u7684\u7528\u6237\u548c\u8fde\u63a5\u72b6\u6001<\/p>\n<p>SELECT * FROM pg_stat_activity;<\/p>\n<p>\u2014 \u663e\u793a\u5f53\u524d\u6570\u636e\u5e93\u7684\u6240\u6709\u6846\u67b6\u3001\u6269\u5c55\u3001\u5e8f\u5217\u548c\u89c6\u56fe\u7b49\u5bf9\u8c61<\/p>\n<p>SELECT * FROM pg_catalog.pg_class WHERE relkind IN (\u2018r\u2019, \u2018v\u2019, \u2018S\u2019, \u2018f\u2019);<\/p>\n<p>\u2014 \u663e\u793a\u5f53\u524d\u6570\u636e\u5e93\u7684\u6240\u6709\u8868\u5b57\u6bb5\u4ee5\u53ca\u5b83\u4eec\u7684\u6570\u636e\u7c7b\u578b\u548c\u7ea6\u675f<\/p>\n<p>SELECT column_name, data_type, character_maximum_length, is_nullable, column_default FROM information_schema.columns WHERE table_name = \u2018table1\u2019;<\/p>\n<p>\u2014 \u663e\u793a\u5f53\u524d\u6570\u636e\u5e93\u7684\u7d22\u5f15\u4fe1\u606f\uff0c\u5305\u62ec\u7d22\u5f15\u540d\u79f0\u3001\u8868\u540d\u79f0\u3001\u7d22\u5f15\u7c7b\u578b\u548c\u4f7f\u7528\u60c5\u51b5<\/p>\n<p>SELECT indexname, relname, amname, idx_scan, idx_tup_read, idx_tup_fetch FROM pg_indexes JOIN pg_class ON pg_class.oid = pg_indexes.indexrelid JOIN pg_am ON pg_am.oid = pg_class.relam WHERE schemaname = \u2018public\u2019;<\/p>\n<p>\u2014 \u663e\u793a\u5f53\u524d\u6570\u636e\u5e93\u6bcf\u4e2a\u8868\u7684\u7d22\u5f15\u6570\u3001\u884c\u6570\u548c\u5e73\u5747\u884c\u5927\u5c0f\u7b49\u4fe1\u606f<\/p>\n<p>SELECT relname, reltuples, relpages, (relpages \/ reltuples) AS avg_size, (SELECT COUNT(*) FROM pg_indexes WHERE pg_indexes.tablename = pg_class.relname) AS index_count FROM pg_class WHERE relkind = \u2018r\u2019;<\/p>\n<p>\u201c`<\/p>\n<p>\u4e86\u89e3\u548c\u7ba1\u7406PostgreSQL\u6570\u636e\u5e93\u662f\u4efb\u4f55\u4f01\u4e1a\u548c\u5f00\u53d1\u8005\u7684\u91cd\u8981\u4efb\u52a1\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u4f18\u5316\u3001\u6269\u5c55\u548c\u5b89\u5168\u5730\u7ef4\u62a4\u60a8\u7684\u6570\u636e\u3002\u901a\u8fc7\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001pgAdmin\uff0c\u6216\u8005\u76f4\u63a5\u4f7f\u7528SQL\u67e5\u8be2\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3\u6709\u5173\u5f53\u524d\u6570\u636e\u5e93\u7684\u5e7f\u6cdb\u4fe1\u606f\uff0c\u5305\u62ec\u8868\u3001\u7d22\u5f15\u3001\u89c6\u56fe\u7b49\u7684\u72b6\u6001\u3001\u6027\u80fd\u548c\u4f7f\u7528\u60c5\u51b5\u3002\u5728\u5b9e\u9645\u751f\u4ea7\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u8fd9\u4e9b\u4fe1\u606f\u8fdb\u884c\u9002\u5f53\u7684\u6570\u636e\u5e93\u8c03\u6574\uff0c\u4ee5\u6ee1\u8db3\u4e1a\u52a1\u9700\u6c42\u3002<\/p>\n<p><strong>\u76f8\u5173\u95ee\u9898\u62d3\u5c55\u9605\u8bfb\uff1a<\/strong><\/p>\n<ul>\n<li>postgresql\u6570\u636e\u5e93\u600e\u4e48\u67e5\u8be2\u6240\u6709\u7684\u5e8f\u5217\u540d<\/li>\n<li>\u5982\u4f55\u67e5\u770bPostgreSQL\u6b63\u5728\u6267\u884c\u7684SQL<\/li>\n<\/ul>\n<h3>postgresql\u6570\u636e\u5e93\u600e\u4e48\u67e5\u8be2\u6240\u6709\u7684\u5e8f\u5217\u540d<\/h3>\n<p>postgresql\u6570\u636e\u5e93\u600e\u4e48\u67e5\u8be2\u6240\u6709\u7684\u5e8f\u5217\u5144\u67af\u540d<\/p>\n<p>postgresql\u4e2d\u4e00\u4e2a\u5e8f\u5217\u5bf9\u8c61\u901a\u5e38\u7528\u4e8e\u4e3a\u884c\u6216\u8005\u8868\u751f\u6210\u552f\u4e00\u7684\u6807\u8bc6\u7b26\u3002<\/p>\n<p>\u67e5\u770b\u5e8f\u5217\uff1a<\/p>\n<p>psql \u7684 \\d \u547d\u4ee4\u8f93\u51fa\u4e00\u4e2a\u6570\u8861\u56e2\u636e\u5e93\u5bf9\u8c61\uff0c\u5305\u62ec Sequence\uff0c\u8868\uff0c\u89c6\u56fe\u548c\u7d22\u5f15\u3002\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 \\ds \u547d\u4ee4\u53ea\u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u7684\u7fa1\u62e6\u6d1e\u6240\u6709\u5e8f\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<p>pigdb-# \\ds<\/p>\n<p>List of relations<\/p>\n<p>Schema |Name| Type | Owner<\/p>\n<p>+++<\/p>\n<p>public | author_ids| sequence | ichexw<\/p>\n<p>public | shipments_ship_id_seq | sequence | ichexw<\/p>\n<p>(2 rows)<\/p>\n<h3>\u5982\u4f55\u67e5\u770bPostgreSQL\u6b63\u5728\u6267\u884c\u7684SQL<\/h3>\n<p>1\u3001\u9996\u5148\u767b\u5f55<\/p>\n<p>sql server<\/p>\n<p> managment\u3002<\/p>\n<p>2\u3001\u7136\u540e\u5728\u6253\u5f00\u7684\u9875\u9762\u4e2d\uff0c\u4f7f\u7528\u9f20\u6807\u53f3\u952e\u5355\u51fb\uff0c\u9009\u62e9\u6d3b\u52a8\u76d1\u89c6\u5668\u3002<\/p>\n<p>3\u3001\u7136\u540e\u5728\u6253\u5f00\u9875\u9762\u4e2d\uff0c\u627e\u5230\u6700\u8fd1\u8017\u8d39\u5927\u91cf\u8d44\u6e90\u7684\u51ef\u679d\u67e5\u8be2\u3002<\/p>\n<p>4\u3001\u90d1\u5b59\u5c40\u70b9\u51fb\u4e0b\u4e09\u89d2\u6309\u94ae\uff0c\u770b\u6267\u884c\u6b21\u6570\u66f4\u5927\u7684\u3002<\/p>\n<p>5\u3001\u6700\u540e\u53f3\u952e\u5355\u51fb\u9009\u62e9\u7f16\u8f91\u67e5\u8be2\u6587\u672c\uff0c\u67e5\u770b<\/p>\n<p>sql\u8bed\u53e5<\/p>\n<p>\uff0c\u5c31\u5b8c\u6210\u558a\u8ba9\u4e86\u3002<\/p>\n<p>\u5173\u4e8epostgresql\u67e5\u770b\u6570\u636e\u5e93\u7684\u4ecb\u7ecd\u5230\u6b64\u5c31\u7ed3\u675f\u4e86\uff0c\u4e0d\u77e5\u9053\u4f60\u4ece\u4e2d\u627e\u5230\u4f60\u9700\u8981\u7684\u4fe1\u606f\u4e86\u5417 \uff1f\u5982\u679c\u4f60\u8fd8\u60f3\u4e86\u89e3\u66f4\u591a\u8fd9\u65b9\u9762\u7684\u4fe1\u606f\uff0c\u8bb0\u5f97\u6536\u85cf\u5173\u6ce8\u672c\u7ad9\u3002<\/p>\n<p><a href=\"http:https:\/\/idc.net\/\">\u9999\u6e2f\u670d\u52a1\u5668<\/a>\u9996\u9009\u540e\u6d6a\u4e91\uff0c2H2G\u9996\u670810\u5143\u5f00\u901a\u3002<br \/>\u540e\u6d6a\u4e91\uff08www.IDC.Net\uff09\u63d0\u4f9b\u7b80\u5355\u597d\u7528\uff0c\u4ef7\u683c\u539a\u9053\u7684\u9999\u6e2f\/\u7f8e\u56fd\u4e91\u670d\u52a1\u5668\u548c\u72ec\u7acb\u670d\u52a1\u5668\u3002IDC+ISP+ICP\u8d44\u8d28\u3002ARIN\u548cAPNIC\u4f1a\u5458\u3002\u6210\u719f\u6280\u672f\u56e2\u961f15\u5e74\u884c\u4e1a\u7ecf\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5f00\u6e90\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u4f01\u4e1a\u548c\u5f00\u53d1\u8005\u7684\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u4f5c\u4e2d\u3002\u5f53\u60a8\u5728\u4f7f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7038],"tags":[],"class_list":["post-125783","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/125783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=125783"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/125783\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=125783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=125783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=125783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}