{"id":96902,"date":"2024-10-20T13:19:17","date_gmt":"2024-10-20T05:19:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96902\/"},"modified":"2024-10-20T13:19:18","modified_gmt":"2024-10-20T05:19:18","slug":"postgresql%e9%83%a8%e7%bd%b2%e5%8f%8a%e7%b0%a1%e5%96%ae%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96902\/","title":{"rendered":"PostgreSQL\u90e8\u7f72\u53ca\u7c21\u55ae\u64cd\u4f5c"},"content":{"rendered":"<h1 id=\"postgresql%e9%83%a8%e7%bd%b2%e5%8f%8a%e7%b0%a1%e5%96%ae%e6%93%8d%e4%bd%9c-QvkuVulqoC\">PostgreSQL\u90e8\u7f72\u53ca\u7c21\u55ae\u64cd\u4f5c<\/h1>\n<p>PostgreSQL\u662f\u4e00\u6b3e\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u5b83\u4ee5\u5176\u7a69\u5b9a\u6027\u3001\u64f4\u5c55\u6027\u548c\u8c50\u5bcc\u7684\u529f\u80fd\u800c\u805e\u540d\uff0c\u9069\u5408\u7528\u65bc\u5f9e\u5c0f\u578b\u5230\u5927\u578b\u7684\u6578\u64da\u5eab\u61c9\u7528\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728\u4f3a\u670d\u5668\u4e0a\u90e8\u7f72PostgreSQL\u4ee5\u53ca\u4e00\u4e9b\u57fa\u672c\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e4%b8%80%e3%80%81postgresql%e7%9a%84%e5%ae%89%e8%a3%9d-QvkuVulqoC\">\u4e00\u3001PostgreSQL\u7684\u5b89\u88dd<\/h2>\n<p>\u5728\u958b\u59cb\u4f7f\u7528PostgreSQL\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5728\u4f3a\u670d\u5668\u4e0a\u9032\u884c\u5b89\u88dd\u3002\u4ee5\u4e0b\u662f\u57fa\u65bcUbuntu\u7cfb\u7d71\u7684\u5b89\u88dd\u6b65\u9a5f\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install postgresql postgresql-contrib<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6aa2\u67e5PostgreSQL\u7684\u72c0\u614b\uff1a<\/p>\n<pre><code>sudo systemctl status postgresql<\/code><\/pre>\n<p>\u5982\u679c\u670d\u52d9\u6b63\u5728\u904b\u884c\uff0c\u5247\u53ef\u4ee5\u9032\u5165PostgreSQL\u7684\u547d\u4ee4\u884c\u754c\u9762\u9032\u884c\u64cd\u4f5c\uff1a<\/p>\n<pre><code>sudo -u postgres psql<\/code><\/pre>\n<h2 id=\"%e4%ba%8c%e3%80%81%e5%9f%ba%e6%9c%ac%e9%85%8d%e7%bd%ae-QvkuVulqoC\">\u4e8c\u3001\u57fa\u672c\u914d\u7f6e<\/h2>\n<p>\u5728\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u5efa\u8b70\u9032\u884c\u4e00\u4e9b\u57fa\u672c\u914d\u7f6e\u4ee5\u63d0\u9ad8\u5b89\u5168\u6027\u548c\u6027\u80fd\u3002\u9996\u5148\uff0c\u60a8\u53ef\u4ee5\u8a2d\u7f6ePostgreSQL\u7684\u7528\u6236\u5bc6\u78bc\uff1a<\/p>\n<pre><code>ALTER USER postgres PASSWORD 'your_password';<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u7de8\u8f2fPostgreSQL\u7684\u914d\u7f6e\u6587\u4ef6\u4ee5\u5141\u8a31\u9060\u7a0b\u9023\u63a5\u3002\u6253\u958b\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/postgresql\/12\/main\/postgresql.conf<\/code><\/pre>\n<p>\u627e\u5230\u4ee5\u4e0b\u884c\u4e26\u9032\u884c\u4fee\u6539\uff1a<\/p>\n<pre><code>#listen_addresses = 'localhost'\nlisten_addresses = '*' <\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u7de8\u8f2fpg_hba.conf\u6587\u4ef6\u4ee5\u8a2d\u7f6e\u8a2a\u554f\u6b0a\u9650\uff1a<\/p>\n<pre><code>sudo nano \/etc\/postgresql\/12\/main\/pg_hba.conf<\/code><\/pre>\n<p>\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u4ee5\u4e0b\u884c\u4ee5\u5141\u8a31\u6240\u6709IP\u5730\u5740\u7684\u8a2a\u554f\uff1a<\/p>\n<pre><code>host    all             all             0.0.0.0\/0            md5<\/code><\/pre>\n<p>\u5b8c\u6210\u5f8c\uff0c\u91cd\u555fPostgreSQL\u670d\u52d9\u4ee5\u61c9\u7528\u66f4\u6539\uff1a<\/p>\n<pre><code>sudo systemctl restart postgresql<\/code><\/pre>\n<h2 id=\"%e4%b8%89%e3%80%81%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c-QvkuVulqoC\">\u4e09\u3001\u57fa\u672c\u64cd\u4f5c<\/h2>\n<p>\u5728PostgreSQL\u4e2d\uff0c\u60a8\u53ef\u4ee5\u57f7\u884c\u5404\u7a2e\u57fa\u672c\u64cd\u4f5c\uff0c\u4f8b\u5982\u5275\u5efa\u6578\u64da\u5eab\u3001\u8868\u548c\u63d2\u5165\u6578\u64da\u3002<\/p>\n<h3 id=\"1-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%ba%ab-QvkuVulqoC\">1. \u5275\u5efa\u6578\u64da\u5eab<\/h3>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5275\u5efa\u4e00\u500b\u65b0\u7684\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>CREATE DATABASE mydatabase;<\/code><\/pre>\n<h3 id=\"2-%e5%89%b5%e5%bb%ba%e8%a1%a8-QvkuVulqoC\">2. \u5275\u5efa\u8868<\/h3>\n<p>\u9032\u5165\u6578\u64da\u5eab\u5f8c\uff0c\u53ef\u4ee5\u5275\u5efa\u8868\uff1a<\/p>\n<pre><code>CREATE TABLE users (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(100),\n    email VARCHAR(100) UNIQUE\n);<\/code><\/pre>\n<h3 id=\"3-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-QvkuVulqoC\">3. \u63d2\u5165\u6578\u64da<\/h3>\n<p>\u4f7f\u7528INSERT\u8a9e\u53e5\u63d2\u5165\u6578\u64da\uff1a<\/p>\n<pre><code>INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');<\/code><\/pre>\n<h3 id=\"4-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-QvkuVulqoC\">4. \u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u4f7f\u7528SELECT\u8a9e\u53e5\u67e5\u8a62\u6578\u64da\uff1a<\/p>\n<pre><code>SELECT * FROM users;<\/code><\/pre>\n<h2 id=\"%e5%9b%9b%e3%80%81%e7%b8%bd%e7%b5%90-QvkuVulqoC\">\u56db\u3001\u7e3d\u7d50<\/h2>\n<p>PostgreSQL\u662f\u4e00\u500b\u5f37\u5927\u7684\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u9069\u5408\u5404\u7a2e\u61c9\u7528\u5834\u666f\u3002\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u5728\u4f3a\u670d\u5668\u4e0a\u90e8\u7f72PostgreSQL\u4e26\u9032\u884c\u57fa\u672c\u64cd\u4f5c\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u7684\u652f\u6301\u6216\u5c0b\u627e\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3PostgreSQL\u7684\u90e8\u7f72\u6d41\u7a0b\u53ca\u57fa\u672c\u64cd\u4f5c\uff0c\u8f15\u9b06\u4e0a\u624b\u6578\u64da\u5eab\u7ba1\u7406\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-96902","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96902","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=96902"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96902\/revisions"}],"predecessor-version":[{"id":96903,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96902\/revisions\/96903"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}