{"id":52131,"date":"2024-10-09T00:06:29","date_gmt":"2024-10-08T16:06:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/52131\/"},"modified":"2024-11-14T10:40:16","modified_gmt":"2024-11-14T02:40:16","slug":"ubuntu-%e5%ae%89%e8%a3%9d-postgresql-%e8%b3%87%e6%96%99%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/52131\/","title":{"rendered":"Ubuntu \u5b89\u88dd PostgreSQL \u8cc7\u6599\u5eab"},"content":{"rendered":"<h1 id=\"ubuntu-%e5%ae%89%e8%a3%9d-postgresql-%e8%b3%87%e6%96%99%e5%ba%ab-pkHVmvtOzr\">Ubuntu \u5b89\u88dd PostgreSQL \u8cc7\u6599\u5eab<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u5b83\u4ee5\u5176\u7a69\u5b9a\u6027\u3001\u64f4\u5c55\u6027\u548c\u8c50\u5bcc\u7684\u529f\u80fd\u800c\u805e\u540d\u3002\u672c\u6587\u5c07\u6307\u5c0e\u60a8\u5982\u4f55\u5728 Ubuntu \u7cfb\u7d71\u4e0a\u5b89\u88dd PostgreSQL \u8cc7\u6599\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u57fa\u672c\u7684\u914d\u7f6e\u548c\u4f7f\u7528\u6280\u5de7\u3002<\/p>\n<h2 id=\"%e5%89%8d%e6%9c%9f%e6%ba%96%e5%82%99-pkHVmvtOzr\">\u524d\u671f\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u5b89\u88dd\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u60a8\u7684 Ubuntu \u7cfb\u7d71\u5df2\u7d93\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u66f4\u65b0\u7cfb\u7d71\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt upgrade<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-postgresql-pkHVmvtOzr\">\u5b89\u88dd PostgreSQL<\/h2>\n<p>\u5b89\u88dd PostgreSQL \u975e\u5e38\u7c21\u55ae\uff0c\u60a8\u53ea\u9700\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo apt install postgresql postgresql-contrib<\/code><\/pre>\n<p>\u9019\u689d\u547d\u4ee4\u5c07\u5b89\u88dd PostgreSQL \u53ca\u5176\u9644\u52a0\u7684\u8ca2\u737b\u6a21\u7d44\uff0c\u9019\u4e9b\u6a21\u7d44\u63d0\u4f9b\u4e86\u984d\u5916\u7684\u529f\u80fd\u548c\u5de5\u5177\u3002<\/p>\n<h2 id=\"%e6%aa%a2%e6%9f%a5-postgresql-%e6%9c%8d%e5%8b%99%e7%8b%80%e6%85%8b-pkHVmvtOzr\">\u6aa2\u67e5 PostgreSQL \u670d\u52d9\u72c0\u614b<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u6aa2\u67e5 PostgreSQL \u670d\u52d9\u7684\u72c0\u614b\uff0c\u4ee5\u78ba\u4fdd\u5b83\u6b63\u5728\u904b\u884c\uff1a<\/p>\n<pre><code>sudo systemctl status postgresql<\/code><\/pre>\n<p>\u5982\u679c\u670d\u52d9\u6b63\u5728\u904b\u884c\uff0c\u60a8\u61c9\u8a72\u6703\u770b\u5230\u985e\u4f3c\u65bc\u201cactive (running)\u201d\u7684\u72c0\u614b\u3002\u5982\u679c\u670d\u52d9\u672a\u555f\u52d5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u555f\u52d5\u5b83\uff1a<\/p>\n<pre><code>sudo systemctl start postgresql<\/code><\/pre>\n<h2 id=\"%e8%a8%ad%e7%bd%ae-postgresql-%e4%bd%bf%e7%94%a8%e8%80%85-pkHVmvtOzr\">\u8a2d\u7f6e PostgreSQL \u4f7f\u7528\u8005<\/h2>\n<p>PostgreSQL \u5b89\u88dd\u5f8c\uff0c\u6703\u81ea\u52d5\u5275\u5efa\u4e00\u500b\u540d\u70ba\u201cpostgres\u201d\u7684\u4f7f\u7528\u8005\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u4f7f\u7528\u8005\u4f86\u7ba1\u7406\u8cc7\u6599\u5eab\u3002\u8981\u9032\u5165 PostgreSQL \u547d\u4ee4\u884c\u754c\u9762\uff0c\u8acb\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo -i -u postgres\npsql<\/code><\/pre>\n<p>\u9032\u5165 PostgreSQL \u547d\u4ee4\u884c\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5275\u5efa\u65b0\u7684\u8cc7\u6599\u5eab\u548c\u4f7f\u7528\u8005\u3002\u4f8b\u5982\uff0c\u5275\u5efa\u4e00\u500b\u540d\u70ba\u201cmydb\u201d\u7684\u8cc7\u6599\u5eab\uff1a<\/p>\n<pre><code>CREATE DATABASE mydb;<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u65b0\u7684\u4f7f\u7528\u8005\u4e26\u6388\u4e88\u5176\u5c0d\u8a72\u8cc7\u6599\u5eab\u7684\u8a2a\u554f\u6b0a\u9650\uff1a<\/p>\n<pre><code>CREATE USER myuser WITH PASSWORD 'mypassword';\nGRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-postgresql-pkHVmvtOzr\">\u914d\u7f6e PostgreSQL<\/h2>\n<p>PostgreSQL \u7684\u914d\u7f6e\u6587\u4ef6\u4f4d\u65bc \/etc\/postgresql\/{version}\/main\/ \u76ee\u9304\u4e0b\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u8981\u9032\u884c\u8abf\u6574\u3002\u6700\u5e38\u898b\u7684\u914d\u7f6e\u6587\u4ef6\u662f <code>postgresql.conf<\/code> \u548c <code>pg_hba.conf<\/code>\u3002<\/p>\n<h3 id=\"%e4%bf%ae%e6%94%b9-postgresql-conf-pkHVmvtOzr\">\u4fee\u6539 postgresql.conf<\/h3>\n<p>\u5728 <code>postgresql.conf<\/code> \u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u8a2d\u7f6e\u8cc7\u6599\u5eab\u7684\u76e3\u807d\u5730\u5740\u3002\u4f8b\u5982\uff0c\u5c07\u5176\u8a2d\u7f6e\u70ba\u76e3\u807d\u6240\u6709\u5730\u5740\uff1a<\/p>\n<pre><code>listen_addresses = '*'<\/code><\/pre>\n<h3 id=\"%e4%bf%ae%e6%94%b9-pg_hba-conf-pkHVmvtOzr\">\u4fee\u6539 pg_hba.conf<\/h3>\n<p>\u5728 <code>pg_hba.conf<\/code> \u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u8a2d\u7f6e\u8a2a\u554f\u63a7\u5236\u898f\u5247\u3002\u60a8\u53ef\u4ee5\u6dfb\u52a0\u4ee5\u4e0b\u884c\u4ee5\u5141\u8a31\u7279\u5b9a IP \u5730\u5740\u7684\u8a2a\u554f\uff1a<\/p>\n<pre><code>host    all             all             192.168.1.0\/24          md5<\/code><\/pre>\n<p>\u5b8c\u6210\u914d\u7f6e\u5f8c\uff0c\u8acb\u91cd\u555f PostgreSQL \u670d\u52d9\u4ee5\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/p>\n<pre><code>sudo systemctl restart postgresql<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-pkHVmvtOzr\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Ubuntu \u7cfb\u7d71\u4e0a\u5b89\u88dd\u548c\u914d\u7f6e PostgreSQL \u8cc7\u6599\u5eab\u3002\u5f9e\u5b89\u88dd\u5230\u57fa\u672c\u7684\u4f7f\u7528\u8005\u548c\u8cc7\u6599\u5eab\u7ba1\u7406\uff0c\u9019\u4e9b\u6b65\u9a5f\u5c07\u5e6b\u52a9\u60a8\u5feb\u901f\u4e0a\u624b PostgreSQL\u3002\u82e5\u60a8\u9700\u8981\u66f4\u9ad8\u6548\u7684\u8cc7\u6599\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u90e8\u7f72\u60a8\u7684 PostgreSQL \u8cc7\u6599\u5eab\uff0c\u4eab\u53d7\u66f4\u7a69\u5b9a\u7684\u6027\u80fd\u548c\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Ubuntu \u4e0a\u5b89\u88dd PostgreSQL \u8cc7\u6599\u5eab\uff0c\u7c21\u55ae\u6b65\u9a5f\u8207\u6307\u5c0e\uff0c\u52a9\u4f60\u5feb\u901f\u642d\u5efa\u7a69\u5b9a\u7684\u6578\u64da\u74b0\u5883\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-52131","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=52131"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52131\/revisions"}],"predecessor-version":[{"id":52132,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52131\/revisions\/52132"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=52131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=52131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=52131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}