{"id":238,"date":"2024-09-21T08:03:05","date_gmt":"2024-09-21T00:03:05","guid":{"rendered":"http:\/\/www.bigqq.com\/?p=238"},"modified":"2026-01-22T09:55:45","modified_gmt":"2026-01-22T01:55:45","slug":"ubuntu-%e5%ae%89%e8%a3%85-pg","status":"publish","type":"post","link":"http:\/\/www.bigqq.com\/?p=238","title":{"rendered":"ubuntu \u5b89\u88c5 PG"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u6587\u4ef6\u5b58\u50a8\u5e93\u914d\u7f6e\n$ sudo sh -c 'echo \"deb http:\/\/apt.postgresql.org\/pub\/repos\/apt $(lsb_release -cs)-pgdg main\" &gt; \/etc\/apt\/sources.list.d\/pgdg.list'\n\u5bfc\u5165\u5b58\u50a8\u5e93\u7b7e\u540d\u5bc6\u94a5\n$ wget --quiet -O - https:\/\/www.postgresql.org\/media\/keys\/ACCC4CF8.asc | sudo apt-key add -\n\napt-key \u8fc7\u65f6\u5904\u7406\uff1a\nwget --quiet -O - https:\/\/www.postgresql.org\/media\/keys\/ACCC4CF8.asc | gpg --dearmor &gt; pgdg.gpg\nsudo mv pgdg.gpg \/etc\/apt\/trusted.gpg.d\/pgdg.gpg\n\nubuntu 22.04 \u66f4\u65b0\u8f6f\u4ef6\u6e90\nsudo cp \/etc\/apt\/sources.list \/etc\/apt\/sources.list.backup\nsudo bash -c \"echo 'deb https:\/\/mirrors.aliyun.com\/ubuntu\/ $(lsb_release -sc) main restricted universe multiverse' &gt; \/etc\/apt\/sources.list\"\nsudo bash -c \"echo 'deb https:\/\/mirrors.aliyun.com\/ubuntu\/ $(lsb_release -sc)-security main restricted universe multiverse' &gt;&gt; \/etc\/apt\/sources.list\"\nsudo bash -c \"echo 'deb https:\/\/mirrors.aliyun.com\/ubuntu\/ $(lsb_release -sc)-updates main restricted universe multiverse' &gt;&gt; \/etc\/apt\/sources.list\"\nsudo bash -c \"echo 'deb https:\/\/mirrors.aliyun.com\/ubuntu\/ $(lsb_release -sc)-backports main restricted universe multiverse' &gt;&gt; \/etc\/apt\/sources.list\"\nsudo apt update\n\n\n\u66f4\u65b0\u8f6f\u4ef6\u5305\u5217\u8868\n$ sudo apt-get update\n\u5b89\u88c5PostgreSQL\n\u5b89\u88c5\u6700\u65b0\u7248\u672cPostgreSQL: sudo apt-get install postgresql\n\u5b89\u88c5\u7279\u5b9a\u7248\u672c:sudo apt-get install postgresql-16\n\n\u5b89\u88c5\u5b8c\u67e5\u770b\u7248\u672cpsql --version\n\u67e5\u770b\u8fd0\u884c\u72b6\u6001\uff1asystemctl status postgresql\n\u505c\u6b62\u670d\u52a1\uff1asystemctl stop postgresql\n\n\u5230\u914d\u7f6e\u76ee\u5f55\uff1acd \/etc\/postgresql\/16\/main\n\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff1avim pg_hba.conf \u589e\u52a0\nhost    replication     all            192.168.31.195\/24        md5\n\u6216\nhost all all 0.0.0.0\/0 md5\n\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff1avim postgresql.conf(<a href=\"http:\/\/www.bigqq.com\/wp-admin\/post.php?post=204&amp;action=edit\">http:\/\/www.bigqq.com\/wp-admin\/post.php?post=204&amp;action=edit<\/a>)\n\u4f7f\u914d\u7f6e\u751f\u6548\uff1a\u91cd\u542f\u670d\u52a1\uff0c\u6216\u8005\u6570\u636e\u5e93\u7ba1\u7406\u5458\u6267\u884cSELECT pg_reload_conf()<\/code><\/pre>\n\n\n\n<p>\u4fee\u6539\u6570\u636e\u6587\u4ef6\u8def\u5f84<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u65b0\u589e\u6587\u4ef6\u5939\uff1amkdir -p pgdata\/postgresql\/16\/main\n\u4fee\u6539\u6587\u4ef6\u5939\u6743\u9650\n chown -R postgres:postgres \/home\/postgresql\n chmod 750 -R \/home\/postgresql\n\n\u590d\u5236\u539f\u6570\u636e\u6587\u4ef6\uff1arsync -av \/var\/lib\/postgresql\/16\/main \/mesdata\/pgdata\/postgresql\/16\n<\/code><\/pre>\n\n\n\n<p>\u4ece\u4e3b\u5e93\u4e2d\u5907\u4efd\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pg_basebackup -h 172.30.253.170  -D \/mesdata\/pgdata\/postgresql\/16\/main -p 5432 -U postgres -Fp -Xs -Pv -R --checkpoint=fast<\/code><\/pre>\n\n\n\n<p>\u8d26\u6237\u5904\u7406<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4e00. \u4fee\u6539\u7528\u6237postgres\u7684\u5bc6\u7801\nPostgreSQL\u6570\u636e\u5e93\u9ed8\u8ba4\u521b\u5efa\u7ba1\u7406\u5458\u8d26\u53f7\uff1apostgres\uff1b\u4fee\u6539\u5176\u5bc6\u7801\uff0c\u4ec5\u9700\u4e00\u4e0b\u4e09\u6b65\uff1a\n\n1\u3001\u9996\u5148\uff0c\u767b\u5f55PostgreSQL\n\nsudo -u postgres psql postgres -p 5432\n2\u3001\u7136\u540e\uff0c\u4fee\u6539\u8d26\u53f7postgres\u7684\u5bc6\u7801\n\nALTER USER postgres WITH PASSWORD 'Lpf65BsDhDNdaJmH';           (---\u4e00\u5b9a\u8981\u52a0\u5206\u53f7!!!!)\n3\u3001\u6700\u540e\uff0c\u9000\u51fapgsql\u5ba2\u6237\u7aef\n\nexit\n\n\u4e8c. \u4fee\u6539linux\u7cfb\u7edfpostgres\u7528\u6237\u7684\u5bc6\u7801\n\n\nPostgreSQL\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u7684linux\u7528\u6237postgres\uff0c\u4fee\u6539\u8be5\u7528\u6237\u5bc6\u7801\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a\n\n1\u3001\u5148\u5220\u9664\u7528\u6237postgres\u7684\u5386\u53f2\u5bc6\u7801\n\nsudo passwd -d postgres\n2\u3001\u91cd\u65b0\u8bbe\u7f6e\u7528\u6237postgres\u7684\u5bc6\u7801\n\nsudo -u postgres passwd\n3\u3001\u6309\u7167\u7cfb\u7edf\u63d0\u793a\uff0c\u8f93\u5165\u4e24\u6b21\u65b0\u7684\u5bc6\u7801\uff0c\u5373\u53ef\u641e\u5b9a\u3002\n\nEnter new UNIX password:\nRetype new UNIX password:\npasswd: password updated successfully<\/code><\/pre>\n\n\n\n<p>\u53c2\u6570\u8c03\u4f18<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;zhengkai.blog.csdn.net\/article\/details\/154301910\n\n\u53c2\u6570\u540d\u79f0\tPG16 \u9ed8\u8ba4\u503c\t\u5efa\u8bae\u4f18\u5316\u503c\t\u53c2\u6570\u542b\u4e49\u4e0e\u8c03\u4f18\u8bf4\u660e\nshared_buffers\t128MB\t64GB\t\ud83d\udd25 \u5171\u4eab\u7f13\u51b2\u533a\uff1a\u6570\u636e\u5e93\u7f13\u5b58\u5927\u5c0f\u3002\u5efa\u8bae\u8bbe\u7f6e\u4e3a\u5185\u5b58\u768425%\uff0c\u5927\u5e45\u51cf\u5c11\u78c1\u76d8I\/O\neffective_cache_size\t4GB\t192GB\t\ud83d\udca1 \u6709\u6548\u7f13\u5b58\uff1a\u544a\u8bc9\u4f18\u5316\u5668\u7cfb\u7edf\u53ef\u7528\u7f13\u5b58\u5927\u5c0f\uff0c\u5f71\u54cd\u7d22\u5f15\u4f7f\u7528\u51b3\u7b56\nrandom_page_cost\t4.0\t1.1\t\u26a1 \u968f\u673a\u9875\u6210\u672c\uff1aSSD\u5b58\u50a8\u5efa\u8bae\u8bbe\u4e3a1.1\uff0cNVMe SSD\u53ef\u8bbe\u4e3a1.0\neffective_io_concurrency\t1\t300\t\ud83d\ude80 I\/O\u5e76\u53d1\u5ea6\uff1a\u9ad8\u901fSSD\u53ef\u5927\u5e45\u63d0\u5347\u6b64\u503c\uff0c\u5145\u5206\u53d1\u6325I\/O\u6027\u80fd\nwork_mem\t4MB\t128MB\t\ud83e\udde0 \u5de5\u4f5c\u5185\u5b58\uff1a\u6bcf\u4e2a\u6392\u5e8f\/\u54c8\u5e0c\u64cd\u4f5c\u53ef\u7528\u5185\u5b58\u3002\u6ce8\u610f\u603b\u6d88\u8017 = work_mem \u00d7 \u64cd\u4f5c\u6570\nmaintenance_work_mem\t64MB\t2GB\t\ud83d\udee0\ufe0f \u7ef4\u62a4\u5185\u5b58\uff1aVACUUM\u3001CREATE INDEX\u7b49\u64cd\u4f5c\u7684\u5185\u5b58\uff0c\u663e\u8457\u52a0\u901f\u7ef4\u62a4\u4efb\u52a1\nmin_wal_size\t80MB\t1GB\t\ud83d\udcdd \u6700\u5c0fWAL\u5927\u5c0f\uff1a\u51cf\u5c11WAL\u6587\u4ef6\u5faa\u73af\uff0c\u5e73\u6ed1I\/O\u64cd\u4f5c\nmax_wal_size\t1GB\t4GB\t\ud83d\udcdd \u6700\u5927WAL\u5927\u5c0f\uff1a\u5927\u5e45\u51cf\u5c11\u68c0\u67e5\u70b9\u9891\u7387\uff0c\u964d\u4f4eI\/O\u538b\u529b\nmax_worker_processes\t8\t16\t\u2699\ufe0f \u6700\u5927\u5de5\u4f5c\u8fdb\u7a0b\uff1a\u8bbe\u7f6e\u4e3aCPU\u6838\u6570\uff0c\u652f\u6301\u66f4\u591a\u5e76\u884c\u64cd\u4f5c\nmax_parallel_workers_per_gather\t2\t8\t\ud83d\udd04 \u5e76\u884c\u5de5\u4f5c\u8fdb\u7a0b\uff1a\u5355\u4e2a\u67e5\u8be2\u7684\u5e76\u884c\u5ea6\uff0c\u5e73\u8861\u5e76\u53d1\u4e0e\u6027\u80fd\nmax_parallel_workers\t8\t16\t\u2699\ufe0f \u6700\u5927\u5e76\u884c\u5de5\u4f5c\u8fdb\u7a0b\uff1a\u7cfb\u7edf\u603b\u5e76\u884c\u5de5\u4f5c\u8fdb\u7a0b\u6570\nmax_parallel_maintenance_workers\t2\t4\t\ud83d\udee0\ufe0f \u7ef4\u62a4\u5e76\u884c\u5ea6\uff1a\u52a0\u901f\u7d22\u5f15\u521b\u5efa\u3001VACUUM\u7b49\u7ef4\u62a4\u64cd\u4f5c\ncheckpoint_completion_target\t0.9\t0.9\t\u23f1\ufe0f \u68c0\u67e5\u70b9\u5b8c\u6210\u76ee\u6807\uff1a\u4fdd\u6301\u9ed8\u8ba40.9\uff0c\u5e73\u6ed1\u68c0\u67e5\u70b9I\/O\n\n\ud83c\udfaf \u5173\u952e\u8865\u5145\u8c03\u4f18\u53c2\u6570\n\u53c2\u6570\u540d\u79f0\tPG16 \u9ed8\u8ba4\u503c\t\u5efa\u8bae\u4f18\u5316\u503c\t\u8c03\u4f18\u8bf4\u660e\nautovacuum_max_workers\t3\t6\t\ud83d\udd27 \u81ea\u52a8\u6e05\u7406\u5de5\u4f5c\u8fdb\u7a0b\uff1a\u589e\u52a0\u6e05\u7406\u8fdb\u7a0b\u6570\uff0c\u5e94\u5bf9\u9ad8\u8d1f\u8f7d\nautovacuum_vacuum_scale_factor\t0.2\t0.1\t\ud83e\uddf9 \u81ea\u52a8\u6e05\u7406\u89e6\u53d1\u6bd4\u4f8b\uff1a\u66f4\u79ef\u6781\u7684\u6e05\u7406\uff0c\u9632\u6b62\u8868\u81a8\u80c0\nautovacuum_analyze_scale_factor\t0.1\t0.05\t\ud83d\udcca \u81ea\u52a8\u5206\u6790\u89e6\u53d1\u6bd4\u4f8b\uff1a\u66f4\u9891\u7e41\u7684\u7edf\u8ba1\u4fe1\u606f\u66f4\u65b0\nwal_buffers\t-1 (\u81ea\u52a8)\t16MB\t\ud83d\udcdd WAL\u7f13\u51b2\u533a\uff1a\u663e\u5f0f\u8bbe\u7f6e\u4e3a16MB\uff0c\u63d0\u5347\u5199\u5165\u6027\u80fd\ndefault_statistics_target\t100\t500\t\ud83d\udcc8 \u7edf\u8ba1\u4fe1\u606f\u76ee\u6807\uff1a\u66f4\u8be6\u7ec6\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u63d0\u5347\u67e5\u8be2\u8ba1\u5212\u8d28\u91cf\nmax_connections\t100\t200-300\t\ud83d\udc65 \u6700\u5927\u8fde\u63a5\u6570\uff1a\u6839\u636e\u5e94\u7528\u9700\u6c42\u8c03\u6574\uff0c\u907f\u514d\u8bbe\u7f6e\u8fc7\u9ad8\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4fee\u6539\u6570\u636e\u6587\u4ef6\u8def\u5f84 \u4ece\u4e3b\u5e93\u4e2d\u5907\u4efd\u6570\u636e\uff1a \u8d26 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.bigqq.com\/index.php?rest_route=\/wp\/v2\/posts\/238"}],"collection":[{"href":"http:\/\/www.bigqq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.bigqq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.bigqq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bigqq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=238"}],"version-history":[{"count":10,"href":"http:\/\/www.bigqq.com\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":397,"href":"http:\/\/www.bigqq.com\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions\/397"}],"wp:attachment":[{"href":"http:\/\/www.bigqq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.bigqq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.bigqq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}