{"id":160665,"date":"2024-11-03T23:59:28","date_gmt":"2024-11-03T15:59:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/160665\/"},"modified":"2024-11-03T23:59:29","modified_gmt":"2024-11-03T15:59:29","slug":"postgresql%e6%95%99%e7%a8%8bpostgresql-%e8%b3%87%e6%96%99%e9%a1%9e%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/160665\/","title":{"rendered":"POSTGRESQL\u6559\u7a0bPostgreSQL \u8cc7\u6599\u985e\u578b"},"content":{"rendered":"<h1 id=\"postgresql%e6%95%99%e7%a8%8b%ef%bc%9apostgresql-%e8%b3%87%e6%96%99%e9%a1%9e%e5%9e%8b-UQaXSMatuQ\">PostgreSQL\u6559\u7a0b\uff1aPostgreSQL \u8cc7\u6599\u985e\u578b<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\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\u5176\u9748\u6d3b\u7684\u8cc7\u6599\u985e\u578b\u7cfb\u7d71\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u6839\u64da\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u8cc7\u6599\u985e\u578b\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e PostgreSQL \u7684\u5404\u7a2e\u8cc7\u6599\u985e\u578b\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728\u8cc7\u6599\u5eab\u8a2d\u8a08\u4e2d\u904b\u7528\u9019\u4e9b\u8cc7\u6599\u985e\u578b\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e8%b3%87%e6%96%99%e9%a1%9e%e5%9e%8b-UQaXSMatuQ\">\u57fa\u672c\u8cc7\u6599\u985e\u578b<\/h2>\n<p>PostgreSQL \u63d0\u4f9b\u4e86\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u985e\u578b\uff0c\u9019\u4e9b\u8cc7\u6599\u985e\u578b\u53ef\u4ee5\u6eff\u8db3\u5927\u591a\u6578\u61c9\u7528\u7684\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u57fa\u672c\u8cc7\u6599\u985e\u578b\uff1a<\/p>\n<ul>\n<li><strong>\u6574\u6578\u985e\u578b\uff1a<\/strong>\u5305\u62ec <code>smallint<\/code>\uff082 bytes\uff09\u3001<code>integer<\/code>\uff084 bytes\uff09\u548c <code>bigint<\/code>\uff088 bytes\uff09\uff0c\u7528\u65bc\u5b58\u5132\u6574\u6578\u503c\u3002<\/li>\n<li><strong>\u6d6e\u9ede\u6578\u985e\u578b\uff1a<\/strong>\u5305\u62ec <code>real<\/code>\uff084 bytes\uff09\u548c <code>double precision<\/code>\uff088 bytes\uff09\uff0c\u7528\u65bc\u5b58\u5132\u5e36\u5c0f\u6578\u7684\u6578\u503c\u3002<\/li>\n<li><strong>\u5b57\u4e32\u985e\u578b\uff1a<\/strong>\u5305\u62ec <code>char(n)<\/code>\u3001<code>varchar(n)<\/code> \u548c <code>text<\/code>\uff0c\u7528\u65bc\u5b58\u5132\u5b57\u7b26\u6578\u64da\u3002<\/li>\n<li><strong>\u5e03\u6797\u985e\u578b\uff1a<\/strong>\u4f7f\u7528 <code>boolean<\/code>\uff0c\u53ef\u5b58\u5132 <code>true<\/code>\u3001<code>false<\/code> \u6216 <code>null<\/code>\u3002<\/li>\n<li><strong>\u65e5\u671f\u548c\u6642\u9593\u985e\u578b\uff1a<\/strong>\u5305\u62ec <code>date<\/code>\u3001<code>time<\/code>\u3001<code>timestamp<\/code> \u548c <code>interval<\/code>\uff0c\u7528\u65bc\u8655\u7406\u6642\u9593\u76f8\u95dc\u7684\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a4%87%e5%90%88%e8%b3%87%e6%96%99%e9%a1%9e%e5%9e%8b-UQaXSMatuQ\">\u8907\u5408\u8cc7\u6599\u985e\u578b<\/h2>\n<p>PostgreSQL \u4e5f\u652f\u6301\u8907\u5408\u8cc7\u6599\u985e\u578b\uff0c\u9019\u4e9b\u8cc7\u6599\u985e\u578b\u5141\u8a31\u7528\u6236\u5b9a\u7fa9\u81ea\u5df1\u7684\u8cc7\u6599\u7d50\u69cb\u3002\u8907\u5408\u8cc7\u6599\u985e\u578b\u7684\u4f8b\u5b50\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u81ea\u5b9a\u7fa9\u985e\u578b\uff1a<\/strong>\u7528\u6236\u53ef\u4ee5\u4f7f\u7528 <code>CREATE TYPE<\/code> \u8a9e\u53e5\u5275\u5efa\u81ea\u5b9a\u7fa9\u8cc7\u6599\u985e\u578b\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre><code>CREATE TYPE address AS (\n    street varchar,\n    city varchar,\n    zip_code varchar\n);<\/code><\/pre>\n<p>\u9019\u6a23\u5c31\u53ef\u4ee5\u5728\u8cc7\u6599\u8868\u4e2d\u4f7f\u7528 <code>address<\/code> \u985e\u578b\u4f86\u5b58\u5132\u5730\u5740\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e9%99%a3%e5%88%97%e8%b3%87%e6%96%99%e9%a1%9e%e5%9e%8b-UQaXSMatuQ\">\u9663\u5217\u8cc7\u6599\u985e\u578b<\/h2>\n<p>PostgreSQL \u5141\u8a31\u7528\u6236\u5275\u5efa\u9663\u5217\u8cc7\u6599\u985e\u578b\uff0c\u9019\u4f7f\u5f97\u5728\u4e00\u500b\u6b04\u4f4d\u4e2d\u5b58\u5132\u591a\u500b\u503c\u8b8a\u5f97\u7c21\u55ae\u3002\u9663\u5217\u53ef\u4ee5\u662f\u4efb\u4f55\u57fa\u672c\u8cc7\u6599\u985e\u578b\u6216\u81ea\u5b9a\u7fa9\u985e\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>CREATE TABLE students (\n    id serial PRIMARY KEY,\n    name varchar(100),\n    grades integer[]\n);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c<code>grades<\/code> \u6b04\u4f4d\u53ef\u4ee5\u5b58\u5132\u4e00\u500b\u6574\u6578\u9663\u5217\uff0c\u8868\u793a\u5b78\u751f\u7684\u6210\u7e3e\u3002<\/p>\n<h2 id=\"json-%e5%92%8c-jsonb-%e8%b3%87%e6%96%99%e9%a1%9e%e5%9e%8b-UQaXSMatuQ\">JSON \u548c JSONB \u8cc7\u6599\u985e\u578b<\/h2>\n<p>\u96a8\u8457\u975e\u7d50\u69cb\u5316\u6578\u64da\u7684\u8208\u8d77\uff0cPostgreSQL \u63d0\u4f9b\u4e86 <code>json<\/code> \u548c <code>jsonb<\/code> \u8cc7\u6599\u985e\u578b\u4f86\u5b58\u5132 JSON \u683c\u5f0f\u7684\u6578\u64da\u3002<code>jsonb<\/code> \u662f\u4e8c\u9032\u5236\u683c\u5f0f\uff0c\u63d0\u4f9b\u66f4\u5feb\u7684\u67e5\u8a62\u6027\u80fd\u548c\u66f4\u597d\u7684\u7d22\u5f15\u652f\u6301\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 JSONB \u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>CREATE TABLE products (\n    id serial PRIMARY KEY,\n    data jsonb\n);<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u5728 <code>data<\/code> \u6b04\u4f4d\u4e2d\u5b58\u5132\u4efb\u610f\u7684 JSON \u6578\u64da\u3002<\/p>\n<h2 id=\"%e5%9c%b0%e7%90%86%e7%a9%ba%e9%96%93%e8%b3%87%e6%96%99%e9%a1%9e%e5%9e%8b-UQaXSMatuQ\">\u5730\u7406\u7a7a\u9593\u8cc7\u6599\u985e\u578b<\/h2>\n<p>PostgreSQL \u9084\u652f\u6301\u5730\u7406\u7a7a\u9593\u8cc7\u6599\u985e\u578b\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5730\u7406\u4fe1\u606f\u7cfb\u7d71\uff08GIS\uff09\u6578\u64da\u7684\u61c9\u7528\u975e\u5e38\u91cd\u8981\u3002\u4f7f\u7528 PostGIS \u64f4\u5c55\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>geometry<\/code> \u548c <code>geography<\/code> \u985e\u578b\u4f86\u5b58\u5132\u5730\u7406\u6578\u64da\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>CREATE TABLE locations (\n    id serial PRIMARY KEY,\n    name varchar(100),\n    geom geography(Point, 4326)\n);<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u5728 <code>geom<\/code> \u6b04\u4f4d\u4e2d\u5b58\u5132\u5730\u7406\u5750\u6a19\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-UQaXSMatuQ\">\u7e3d\u7d50<\/h2>\n<p>PostgreSQL \u63d0\u4f9b\u4e86\u591a\u7a2e\u8cc7\u6599\u985e\u578b\uff0c\u5f9e\u57fa\u672c\u7684\u6574\u6578\u548c\u5b57\u4e32\u5230\u8907\u96dc\u7684\u81ea\u5b9a\u7fa9\u985e\u578b\u548c\u5730\u7406\u7a7a\u9593\u8cc7\u6599\u985e\u578b\uff0c\u9019\u4e9b\u90fd\u4f7f\u5f97 PostgreSQL \u6210\u70ba\u4e00\u500b\u9748\u6d3b\u4e14\u5f37\u5927\u7684\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u4e86\u89e3\u9019\u4e9b\u8cc7\u6599\u985e\u578b\u7684\u7279\u6027\u548c\u4f7f\u7528\u5834\u666f\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u8a2d\u8a08\u8cc7\u6599\u5eab\u7d50\u69cb\uff0c\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u8cc7\u6599\u5eab\u7ba1\u7406\u9084\u662f\u61c9\u7528\u90e8\u7f72\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22PostgreSQL\u8cc7\u6599\u985e\u578b\u7684\u5168\u9762\u6559\u7a0b\uff0c\u4e86\u89e3\u5404\u7a2e\u6578\u64da\u985e\u578b\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u63d0\u5347\u6578\u64da\u5eab\u7ba1\u7406\u6280\u80fd\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-160665","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160665","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=160665"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160665\/revisions"}],"predecessor-version":[{"id":160666,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160665\/revisions\/160666"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=160665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=160665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=160665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}