{"id":2587,"date":"2025-03-09T19:04:26","date_gmt":"2025-03-09T11:04:26","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2587"},"modified":"2025-03-09T19:21:13","modified_gmt":"2025-03-09T11:21:13","slug":"postgresql-%e9%80%89%e6%8b%a9%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/09\/postgresql-%e9%80%89%e6%8b%a9%e6%95%b0%e6%8d%ae%e5%ba%93\/","title":{"rendered":"PostgreSQL \u9009\u62e9\u6570\u636e\u5e93"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c\u9009\u62e9\u6570\u636e\u5e93\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u5f53\u4f60\u8fde\u63a5\u5230 PostgreSQL \u5b9e\u4f8b\u65f6\uff0c\u9996\u5148\u4f1a\u8fde\u63a5\u5230\u4e00\u4e2a\u9ed8\u8ba4\u7684\u6570\u636e\u5e93\uff0c\u901a\u5e38\u662f <code>postgres<\/code>\u3002\u7136\u800c\uff0c\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u4f60\u901a\u5e38\u4f1a\u6839\u636e\u9700\u8981\u5207\u6362\u5230\u7279\u5b9a\u7684\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u4f7f\u7528 <code>psql<\/code> \u8fde\u63a5\u5230 PostgreSQL \u5b9e\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.1 \u521d\u59cb\u8fde\u63a5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u7b2c\u4e00\u6b21\u8fde\u63a5\u5230 PostgreSQL \u5b9e\u4f8b\u65f6\uff0c\u901a\u5e38\u4f1a\u6307\u5b9a\u4e00\u4e2a\u6570\u636e\u5e93\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u660e\u786e\u6307\u5b9a\u6570\u636e\u5e93\u540d\u79f0\uff0c<code>psql<\/code> \u4f1a\u8fde\u63a5\u5230\u540d\u4e3a <code>postgres<\/code> \u7684\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>psql -U postgres<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u8ff0\u547d\u4ee4\u4f1a\u4ee5 <code>postgres<\/code> \u7528\u6237\u8eab\u4efd\u8fde\u63a5\u5230 PostgreSQL \u5b9e\u4f8b\u3002\u5982\u679c\u4f60\u6ca1\u6709\u6307\u5b9a\u6570\u636e\u5e93\uff0c\u5b83\u4f1a\u81ea\u52a8\u8fde\u63a5\u5230 <code>postgres<\/code> \u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.2 \u8fde\u63a5\u5230\u7279\u5b9a\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u5728\u8fde\u63a5\u65f6\u6307\u5b9a\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8fde\u63a5\u5230\u540d\u4e3a <code>mydatabase<\/code> \u7684\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>psql -U postgres -d mydatabase<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6216\u8005\u7b80\u5199\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>psql mydatabase<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4f1a\u8fde\u63a5\u5230 <code>mydatabase<\/code> \u6570\u636e\u5e93\uff0c\u5e76\u8fdb\u5165 <code>psql<\/code> \u63d0\u793a\u7b26\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u5728 <code>psql<\/code> \u4e2d\u9009\u62e9\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5df2\u7ecf\u8fde\u63a5\u5230 PostgreSQL \u5b9e\u4f8b\uff0c\u5e76\u4e14\u5e0c\u671b\u5207\u6362\u5230\u5176\u4ed6\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>\\c<\/code>\uff08\u6216 <code>\\connect<\/code>\uff09\u547d\u4ee4\u3002\u8fd9\u4e2a\u547d\u4ee4\u5141\u8bb8\u4f60\u5728\u4e0d\u9000\u51fa <code>psql<\/code> \u4f1a\u8bdd\u7684\u60c5\u51b5\u4e0b\u5207\u6362\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.1 \u5207\u6362\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\\c dbname<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dbname<\/code> \u662f\u4f60\u60f3\u5207\u6362\u5230\u7684\u76ee\u6807\u6570\u636e\u5e93\u7684\u540d\u79f0\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5df2\u7ecf\u8fde\u63a5\u5230 <code>postgres<\/code> \u6570\u636e\u5e93\uff0c\u5e76\u4e14\u60f3\u5207\u6362\u5230 <code>mydatabase<\/code> \u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\c mydatabase<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u540e\uff0c\u7cfb\u7edf\u4f1a\u663e\u793a\u7c7b\u4f3c\u4e0b\u9762\u7684\u8f93\u51fa\uff0c\u8868\u793a\u5207\u6362\u6210\u529f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>You are now connected to database \"mydatabase\" as user \"postgres\".<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2 \u4f7f\u7528\u4e0d\u540c\u7684\u7528\u6237\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u4e5f\u53ef\u4ee5\u5728\u5207\u6362\u6570\u636e\u5e93\u65f6\u6307\u5b9a\u4e0d\u540c\u7684\u7528\u6237\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\c mydatabase newuser<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4f1a\u5c1d\u8bd5\u4ee5 <code>newuser<\/code> \u7528\u6237\u8eab\u4efd\u8fde\u63a5\u5230 <code>mydatabase<\/code> \u6570\u636e\u5e93\u3002\u5982\u679c <code>newuser<\/code> \u7528\u6237\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u8be5\u6570\u636e\u5e93\uff0c\u8fde\u63a5\u4f1a\u5931\u8d25\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.3 \u67e5\u770b\u5f53\u524d\u8fde\u63a5\u7684\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u67e5\u770b\u5f53\u524d\u8fde\u63a5\u7684\u662f\u54ea\u4e2a\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>\\conninfo<\/code> \u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\conninfo<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u8f93\u51fa\u5f53\u524d\u8fde\u63a5\u4fe1\u606f\uff0c\u5305\u62ec\u6570\u636e\u5e93\u540d\u79f0\u3001\u8fde\u63a5\u7528\u6237\u548c\u8fde\u63a5\u4e3b\u673a\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>You are connected to database \"mydatabase\" as user \"postgres\" via socket in \"\/var\/run\/postgresql\" at port 5432.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u901a\u8fc7 SQL \u8bed\u53e5\u9009\u62e9\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL \u4e0d\u50cf\u67d0\u4e9b\u6570\u636e\u5e93\u7cfb\u7edf\uff08\u4f8b\u5982 MySQL\uff09\u90a3\u6837\u5141\u8bb8\u901a\u8fc7 SQL \u8bed\u53e5\u76f4\u63a5\u5207\u6362\u6570\u636e\u5e93\u3002\u4f60\u4e0d\u80fd\u4f7f\u7528\u7c7b\u4f3c <code>USE mydatabase;<\/code> \u7684\u547d\u4ee4\u6765\u5207\u6362\u6570\u636e\u5e93\u3002\u4f60\u5fc5\u987b\u901a\u8fc7 <code>psql<\/code> \u547d\u4ee4\u884c\u5de5\u5177\u4e2d\u7684 <code>\\c<\/code> \u547d\u4ee4\u6765\u5207\u6362\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u5217\u51fa\u6240\u6709\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u67e5\u770b\u5f53\u524d PostgreSQL \u5b9e\u4f8b\u4e2d\u7684\u6240\u6709\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>\\l<\/code> \u6216 <code>\\list<\/code> \u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\l<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u5217\u51fa\u6240\u6709\u53ef\u7528\u7684\u6570\u636e\u5e93\uff0c\u4ee5\u53ca\u5b83\u4eec\u7684\u6240\u6709\u8005\u3001\u7f16\u7801\u548c\u8bbf\u95ee\u6743\u9650\u7b49\u4fe1\u606f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                              List of databases\n   Name    |  Owner   | Encoding |  Collation  |   Ctype   | Access privileges \n-----------+----------+----------+-------------+-----------+-------------------\n mydatabase | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | \n postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | \n template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c\/postgres     \n template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c\/postgres     \n(4 rows)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u5220\u9664\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c\u5982\u679c\u4f60\u4e0d\u518d\u9700\u8981\u67d0\u4e2a\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>DROP DATABASE<\/code> \u547d\u4ee4\u5220\u9664\u5b83\u3002\u5220\u9664\u6570\u636e\u5e93\u662f\u4e00\u4e2a\u4e0d\u53ef\u6062\u590d\u7684\u64cd\u4f5c\uff0c\u6267\u884c\u540e\u6570\u636e\u5e93\u53ca\u5176\u4e2d\u7684\u6240\u6709\u6570\u636e\u5c06\u4e22\u5931\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP DATABASE dbname;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u5220\u9664\u540d\u4e3a <code>mydatabase<\/code> \u7684\u6570\u636e\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP DATABASE mydatabase;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f<\/strong>\uff1a\u4f60\u5fc5\u987b\u786e\u4fdd\u5728\u5220\u9664\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u6ca1\u6709\u5176\u4ed6\u8fde\u63a5\u5230\u8be5\u6570\u636e\u5e93\u7684\u4f1a\u8bdd\u3002\u5982\u679c\u6709\u5176\u4ed6\u4f1a\u8bdd\u8fde\u63a5\uff0c\u4f60\u9700\u8981\u5148\u65ad\u5f00\u5b83\u4eec\uff0c\u6216\u8005\u4f7f\u7528 <code>pg_terminate_backend<\/code> \u51fd\u6570\u624b\u52a8\u7ec8\u6b62\u8fde\u63a5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u901a\u8fc7 pgAdmin \u9009\u62e9\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u4f7f\u7528 <code>pgAdmin<\/code> \u56fe\u5f62\u5316\u754c\u9762\u7ba1\u7406 PostgreSQL\uff0c\u53ef\u4ee5\u8f7b\u677e\u5207\u6362\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6b65\u9aa4\uff1a<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u542f\u52a8 <code>pgAdmin<\/code> \u5e76\u8fde\u63a5\u5230 PostgreSQL \u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li>\u5728\u5de6\u4fa7\u7684\u6811\u72b6\u89c6\u56fe\u4e2d\uff0c\u627e\u5230\u5e76\u5c55\u5f00 &#8220;Databases&#8221;\uff08\u6570\u636e\u5e93\uff09\u8282\u70b9\u3002<\/li>\n\n\n\n<li>\u53f3\u952e\u70b9\u51fb\u4f60\u60f3\u9009\u62e9\u7684\u6570\u636e\u5e93\uff0c\u9009\u62e9 &#8220;Connect&#8221;\uff08\u8fde\u63a5\uff09\u6765\u5207\u6362\u5230\u8be5\u6570\u636e\u5e93\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728 <code>psql<\/code> \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>\\c dbname<\/code> \u5207\u6362\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li>PostgreSQL \u4e0d\u652f\u6301\u901a\u8fc7 SQL \u8bed\u53e5\u76f4\u63a5\u5207\u6362\u6570\u636e\u5e93\uff0c\u5fc5\u987b\u4f7f\u7528 <code>psql<\/code> \u7684\u547d\u4ee4\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>\\l<\/code> \u67e5\u770b\u5f53\u524d PostgreSQL \u5b9e\u4f8b\u4e2d\u7684\u6240\u6709\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>DROP DATABASE<\/code> \u5220\u9664\u6570\u636e\u5e93\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u8be5\u64cd\u4f5c\u4e0d\u53ef\u6062\u590d\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u6709\u4efb\u4f55\u7591\u95ee\u6216\u60f3\u8981\u8fdb\u4e00\u6b65\u4e86\u89e3 PostgreSQL \u7684\u5176\u4ed6\u529f\u80fd\uff0c\u968f\u65f6\u544a\u8bc9\u6211\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PostgreSQL \u4e2d\uff0c\u9009\u62e9\u6570\u636e\u5e93\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u5f53\u4f60\u8fde\u63a5\u5230 PostgreSQL \u5b9e\u4f8b\u65f6\uff0c\u9996\u5148\u4f1a\u8fde\u63a5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2591,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2587","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2587"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2587\/revisions"}],"predecessor-version":[{"id":2589,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2587\/revisions\/2589"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2591"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}