1
0
wiki/Technology/DatabaseSystem/PostgreSQL/安装配置.html

1164 lines
570 KiB
HTML
Raw Permalink Normal View History

2024-10-10 16:56:32 +08:00
<!DOCTYPE html>
<html lang="zh"><head><title>安装配置</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Noto Serif Simplified Chinese:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="安装配置"/><meta property="og:description" content="Postgresql 安装配置."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../static/icon.png"/><meta name="description" content="Postgresql 安装配置."/><meta name="generator" content="Quartz"/><link href="../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/DatabaseSystem/PostgreSQL/安装配置"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" vie
<p>对于初学者和有经验的开发者来说,正确地安装和配置 PostgreSQL 是至关重要的。一个恰当的配置不仅可以确保数据库的稳定运行,还可以充分发挥其性能,满足复杂的业务需求。然而,由于 PostgreSQL 的功能丰富和配置项繁多,很多人在安装和配置过程中可能会遇到困惑和挑战。</p>
<p>PostgreSQL 提供了多种安装方式,其中源码安装和编译安装是最为基础和原始的方法。这两种方法虽然相对复杂,但为用户提供了极大的灵活性,允许他们根据自己的需求进行定制和优化。</p>
<h2 id="依赖">依赖<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#依赖" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<div class="table-container"><table><thead><tr><th>依赖</th><th>版本</th><th>用途</th><th>状态</th><th>备注</th></tr></thead><tbody><tr><td>GNU make</td><td>3.81 或更高</td><td>构建 PostgreSQL</td><td></td><td>必需; 其他版本或其他 make 程序不可用</td></tr><tr><td>ISO/ANSI C compiler</td><td>C99 兼容</td><td>编译 PostgreSQL</td><td></td><td>必需; 推荐使用 GCC 的最新版本</td></tr><tr><td>tar</td><td>无特定版本</td><td>解压源代码分发包</td><td></td><td>必需; 需要配合 gzip 或 bzip2 使用</td></tr><tr><td>GNU Readline library</td><td>无特定版本</td><td>提高 PostgreSQL 命令行 SQL 解释器的用户体验</td><td></td><td>必需; 可以选择不使用或使用 libedit 库作为替代</td></tr><tr><td>zlib</td><td>无特定版本</td><td>支持 pg_dump 和 pg_restore 中的压缩存档功能</td><td></td><td>必需; 可以选择不使用</td></tr><tr><td>Perl</td><td>5.8.3 或更高</td><td>构建 PL/Perl 服务器编程语言</td><td>🟢</td><td>可选; 需要 libperl 共享库和头文件</td></tr><tr><td>Python</td><td>3.2 或更高</td><td>构建 PL/Python 服务器编程语言</td><td>🟢</td><td>可选; 需要 libpython 共享库和头文件</td></tr><tr><td>Tcl</td><td>8.4 或更高</td><td>构建 PL/Tcl 程序语言</td><td>🟢</td><td>可选; 需要 Tcl 安装</td></tr><tr><td>OpenSSL</td><td>1.0.1 或更高</td><td>支持加密的客户端连接和随机数生成</td><td>🟢</td><td>可选; 用于支持加密的客户端连接和随机数生成</td></tr><tr><td>Gettext API</td><td>无特定版本</td><td>实现 Native Language Support (NLS)</td><td>🟢</td><td>可选; 用于显示程序的非英语消息</td></tr><tr><td>Kerberos</td><td>无特定版本</td><td>支持 Kerberos 认证</td><td>🟢</td><td>可选; 用于支持 Kerberos 认证</td></tr><tr><td>OpenLDAP</td><td>无特定版本</td><td>支持 OpenLDAP 认证</td><td>🟢</td><td>可选; 用于支持 OpenLDAP 认证</td></tr><tr><td>PAM</td><td>无特定版本</td><td>支持 PAM 认证</td><td>🟢</td><td>可选; 用于支持 PAM 认证</td></tr><tr><td>LZ4</td><td>无特定版本</td><td>支持 LZ4 数据压缩</td><td>🟢</td><td>可选; 用于支持 LZ4 数据压缩</td></tr><tr><td>Zstandard</td><td>1.4.0 或更高</td><td>支持 Zstandard 数据压缩</td><td>🟢</td><td>可选; 用于支持 Zstandard 数据压缩</td></tr></tbody></table></div>
<ul>
<li>✅ 表示必需</li>
<li>🟢 表示可选</li>
</ul>
<h3 id="基本依赖">基本依赖<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#基本依赖" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># YUM 更新软件仓库 &amp;&amp; 安装必需的依赖</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> yum</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> &amp;&amp; </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> yum</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tar</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gzip</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> bzip2</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> readline</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> readline-devel</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> zlib</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> zlib-devel</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># APT 更新软件仓库 &amp;&amp; 安装必需的依赖</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> &amp;&amp; </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tar</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gzip</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> bzip2</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libreadline-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> zlib1g-dev</span></span></code></pre></figure>
<h3 id="拓展依赖">拓展依赖<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#拓展依赖" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># YUM 更新软件仓库 &amp;&amp; 安装可选的依赖</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> yum</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> &amp;&amp; </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> yum</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> perl</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> perl-devel</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3-devel</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tcl</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tcl-devel</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> openssl</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> openssl-devel</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gettext</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> krb5</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> krb5-devel</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> openldap</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> openldap-devel</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> pam</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> pam-devel</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> lz4</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> lz4-devel</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> zstd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> zstd-devel</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># APT 更新软件仓库 &amp;&amp; 安装可选的依赖</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> &amp;&amp; </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> perl</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libperl-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tcl</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tcl-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libssl-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gettext</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> krb5-user</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libkrb5-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libldap2-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libpam0g-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> liblz4-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libzstd-dev</span></span></code></pre></figure>
<h3 id="依赖检查">依赖检查<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#依赖检查" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#!/bin/bash</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 检查包管理器</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 可自定义依赖检查项目</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">if</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> command</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> yum</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> &amp;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /dev/null; </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">then</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PACKAGE_MANAGER</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;yum&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> REQUIRED_DEPENDENCIES</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;make&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;gcc&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;tar&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;gzip&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;bzip2&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;readline&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;readline-devel&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;zlib&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;zlib-devel&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> OPTIONAL_DEPENDENCIES</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;perl&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;perl-devel&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;python3&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;python3-devel&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;tcl&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;tcl-devel&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;openssl&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;openssl-devel&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;gettext&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;krb5&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;krb5-devel&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;openldap&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;openldap-devel&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;pam&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;pam-devel&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;lz4&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;lz4-devel&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;zstd&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;zstd-devel&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">elif</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> command</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt-get</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> &amp;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /dev/null; </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">then</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PACKAGE_MANAGER</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;apt-get&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> REQUIRED_DEPENDENCIES</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;make&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;gcc&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;tar&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;gzip&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;bzip2&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;libreadline-dev&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;zlib1g-dev&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> OPTIONAL_DEPENDENCIES</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;perl&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;libperl-dev&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;python3&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;python3-dev&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;tcl&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;tcl-dev&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;libssl-dev&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;gettext&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;krb5-user&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;libkrb5-dev&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;libldap2-dev&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;libpam0g-dev&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;liblz4-dev&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;libzstd-dev&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;❌ 不支持的包管理器。&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> exit</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">fi</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义一个函数来检查依赖是否安装,并显示版本信息</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">check_dependency</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> local</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> package_name</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">$1</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [ </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-z</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$package_name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]; </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">then</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;错误:未提供依赖项名称。&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> exit</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> fi</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$PACKAGE_MANAGER</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ==</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;yum&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]; </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">then</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rpm</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -q</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $package_name &amp;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /dev/null; </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">then</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> version</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">rpm</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -q</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $package_name)</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> printf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot; ✅ %-20s 版本: %s\n&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $package_name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$version</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> else</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> printf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot; ❌ %-20s 未安装\n&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $package_name</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> fi</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> elif</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$PACKAGE_MANAGER</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ==</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;apt-get&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]; </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">then</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> dpkg</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -s</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $package_name &amp;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /dev/null; </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">then</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> version</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">dpkg</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -s</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $package_name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> grep</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> Version</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> |</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> cut</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">' '</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -f2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [ </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-z</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$version</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]; </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">then</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> version</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;未知&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> fi</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> printf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot; ✅ %-20s 版本: %s\n&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $package_name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$version</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> else</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> printf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot; ❌ %-20s 未安装\n&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $package_name</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> fi</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> fi</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义一个函数来检查依赖项数组</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">check_dependencies</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> local</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> dependencies</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">$@</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> for</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> dependency </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">in</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;${</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">dependencies</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">[</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">@</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">]}&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">do</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> check_dependency</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$dependency</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> done</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 检查必需的依赖</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Postgres 依赖检查&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;=> 必需的依赖:&quot;</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">check_dependencies</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;${</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">REQUIRED_DEPENDENCIES</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">[</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">@</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">]}&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 检查可选的依赖</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;=> 可选的依赖:&quot;</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">check_dependencies</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;${</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">OPTIONAL_DEPENDENCIES</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">[</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">@</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">]}&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 输出检查完成信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;=> 依赖检查完成!&quot;</span></span></code></pre></figure>
<h2 id="安装">安装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#安装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="源码安装">源码安装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#源码安装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="1-准备工作">1. 准备工作<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#1-准备工作" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建用户</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> adduser</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> passwd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建目录</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> mkdir</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -p</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /opt/postgresql/{data,archive,scripts,backup,pg16,source,logs,config,temp,extensions,upgrade_scripts}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 配置目录权限,后续默认以 postgres 用户操作</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> chown</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -R</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres:postgres</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /opt/postgresql</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> chmod</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -R</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 775</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /opt/postgresql</span></span></code></pre></figure>
<p>postgresql 目录下文件夹其对应的用途。</p>
<div class="table-container"><table><thead><tr><th style="text-align:left;">目录</th><th style="text-align:left;">用途</th></tr></thead><tbody><tr><td style="text-align:left;">/data</td><td style="text-align:left;">存储 PostgreSQL 数据</td></tr><tr><td style="text-align:left;">/archive</td><td style="text-align:left;">存储归档日志文件</td></tr><tr><td style="text-align:left;">/scripts</td><td style="text-align:left;">存储 PostgreSQL 脚本</td></tr><tr><td style="text-align:left;">/backup</td><td style="text-align:left;">存储数据库备份</td></tr><tr><td style="text-align:left;">/pgsql</td><td style="text-align:left;">软连接到 /pg16</td></tr><tr><td style="text-align:left;">/pg16</td><td style="text-align:left;">PostgreSQL 16 版本</td></tr><tr><td style="text-align:left;">/source</td><td style="text-align:left;">存储源代码或配置文件</td></tr><tr><td style="text-align:left;">/logs</td><td style="text-align:left;">存储 PostgreSQL 日志</td></tr><tr><td style="text-align:left;">/config</td><td style="text-align:left;">存储 PostgreSQL 配置</td></tr><tr><td style="text-align:left;">/temp</td><td style="text-align:left;">存储临时文件</td></tr><tr><td style="text-align:left;">/extensions</td><td style="text-align:left;">存储 PostgreSQL 扩展</td></tr><tr><td style="text-align:left;">/upgrade_scripts</td><td style="text-align:left;">存储升级脚本</td></tr></tbody></table></div>
<h4 id="2-下载源码">2. 下载源码<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#2-下载源码" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>PostgreSQL 源码可以在官方提供的 <a href="https://www.postgresql.org/ftp/source/" class="external">FTP 渠道<svg aria-hidden="true" class="external-icon" viewBox="0 0 512 512"><path d="M320 0H288V64h32 82.7L201.4 265.4 178.7 288 224 333.3l22.6-22.6L448 109.3V192v32h64V192 32 0H480 320zM32 32H0V64 480v32H32 456h32V480 352 320H424v32 96H64V96h96 32V32H160 32z"></path></svg></a> 下载。</p>
<p><img src="https://static.7wate.com/img/2023/09/22/7f25aaababea4.png" alt="PostgreSQL 源码"/></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 切换用户</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">su</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> -</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 下载 PG16 </span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">postgres@debian:/opt/postgresql/source$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> wget</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> https://ftp.postgresql.org/pub/source/v16.0/postgresql-16.0.tar.gz</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 解压 PG16</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">postgres@debian:/opt/postgresql/source$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tar</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -zxvf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgresql-16.0.tar.gz</span></span></code></pre></figure>
<h4 id="3-配置源码">3. 配置源码<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#3-配置源码" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>首先,你需要配置源代码树以适应你的系统并选择你想要的选项。这是通过运行 <code>configure</code> 脚本完成的。你可以使用以下命令进行默认安装:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 安装 /opt/postgresql/pg16/ 路径</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">postgres@debian:/opt/postgresql/source/postgresql-16.0$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ./configure</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --prefix=/opt/postgresql/pg16/</span></span></code></pre></figure>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 默认安装</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./configure</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 指定多个选项</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./configure</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --prefix=/usr/local/pgsql</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --bindir=/usr/local/pgsql/bin</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --datadir=/usr/local/pgsql/data</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --libdir=/usr/local/pgsql/lib</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 禁用某些功能</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./configure</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --without-readline</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --without-zlib</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启用调试和指定编译器</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./configure</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --enable-debug</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --with-pgport=5432</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> CC=gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> CFLAGS='-O2 -g'</span></span></code></pre></figure>
<div class="table-container"><table><thead><tr><th style="text-align:left;">选项</th><th style="text-align:left;">描述</th></tr></thead><tbody><tr><td style="text-align:left;"><code>--prefix=&lt;目录></code></td><td style="text-align:left;">指定安装目录的前缀,即 PostgreSQL 将被安装到的基本目录。</td></tr><tr><td style="text-align:left;"><code>--bindir=&lt;目录></code></td><td style="text-align:left;">指定可执行文件的目录,例如 <code>pg_ctl</code><code>psql</code></td></tr><tr><td style="text-align:left;"><code>--sbindir=&lt;目录></code></td><td style="text-align:left;">指定系统管理员可执行文件的目录,例如 <code>pg_ctl</code><code>pg_config</code></td></tr><tr><td style="text-align:left;"><code>--datadir=&lt;目录></code></td><td style="text-align:left;">指定数据文件的根目录,例如数据表空间。</td></tr><tr><td style="text-align:left;"><code>--libdir=&lt;目录></code></td><td style="text-align:left;">指定库文件的目录,例如共享库文件。</td></tr><tr><td style="text-align:left;"><code>--sysconfdir=&lt;目录></code></td><td style="text-align:left;">指定配置文件的目录。</td></tr><tr><td style="text-align:left;"><code>--docdir=&lt;目录></code></td><td style="text-align:left;">指定文档文件的目录。</td></tr><tr><td style="text-align:left;"><code>--includedir=&lt;目录></code></td><td style="text-align:left;">指定包含头文件的目录,用于开发 PostgreSQL 扩展。</td></tr><tr><td style="text-align:left;"><code>--with-openssl</code></td><td style="text-align:left;">启用 OpenSSL 支持,用于加密通信。</td></tr><tr><td style="text-align:left;"><code>--with-zlib</code></td><td style="text-align:left;">启用 Zlib 支持,用于数据压缩。</td></tr><tr><td style="text-align:left;"><code>--with-libxml</code></td><td style="text-align:left;">启用 Libxml2 支持,用于 XML 数据类型。</td></tr><tr><td style="text-align:left;"><code>--with-python</code></td><td style="text-align:left;">启用 Python 支持,用于编写存储过程和触发器。</td></tr><tr><td style="text-align:left;"><code>--with-perl</code></td><td style="text-align:left;">启用 Perl 支持,用于编写存储过程和触发器。</td></tr><tr><td style="text-align:left;"><code>--with-tcl</code></td><td style="text-align:left;">启用 Tcl 支持,用于编写存储过程和触发器。</td></tr><tr><td style="text-align:left;"><code>--with-pam</code></td><td style="text-align:left;">启用 PAM (Pluggable Authentication Modules) 支持。</td></tr><tr><td style="text-align:left;"><code>--with-ldap</code></td><td style="text-align:left;">启用 LDAP 支持,用于身份验证和用户管理。</td></tr><tr><td style="text-align:left;"><code>--with-krb5</code></td><td style="text-align:left;">启用 Kerberos 支持,用于身份验证。</td></tr><tr><td style="text-align:left;"><code>--with-system-tzdata</code></td><td style="text-align:left;">使用操作系统提供的时区信息。</td></tr><tr><td style="text-align:left;"><code>--enable-debug</code></td><td style="text-align:left;">启用调试支持,构建时包括调试信息。</td></tr><tr><td style="text-align:left;"><code>--disable-ipv6</code></td><td style="text-align:left;">禁用 IPv6 支持。</td></tr></tbody></table></div>
<h4 id="4-构建二进制">4. 构建二进制<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#4-构建二进制" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以输入以下任一命令开始构建,可以使用 -j 选项指定线程数加快速度。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 多线程构建</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -j</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 8</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span></code></pre></figure>
<p>如果你想构建所有可以构建的内容包括文档HTML 和 man 页面和附加模块contrib则可以输入</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> world</span></span></code></pre></figure>
<p>或者如果你想构建所有可以构建的内容包括附加模块contrib但不包括文档可以输入</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> world-bin</span></span></code></pre></figure>
<h4 id="5-测试软件">5. 测试软件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#5-测试软件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>如果你想在安装之前测试新构建的服务器,你可以在此时运行回归测试。这是一个测试套件,用于验证 PostgreSQL 是否按照开发人员预期的方式在你的机器上运行。可以使用以下命令进行测试:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> check</span></span></code></pre></figure>
<h4 id="6-安装">6. 安装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#6-安装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>要安装 PostgreSQL可以输入</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span></span></code></pre></figure>
<p>此步骤将把文件安装到第一步中指定的目录中。**确保你有适当的权限写入该区域。**或者,你可以提前创建目标目录并安排授予适当的权限。</p>
<p>如果你构建了上面的 world可以输入</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install-world</span></span></code></pre></figure>
<p>这也会安装文档。如果你在上面构建了不包含文档的 world可以输入</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install-world-bin</span></span></code></pre></figure>
<h4 id="7-配置软连">7. 配置软连<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#7-配置软连" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>设置 postgres 软链接,方便系统后续升级。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">ln</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /opt/postgres/pg16</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /opt/postgresql/pgsql</span></span></code></pre></figure>
<h4 id="8-卸载和清理">8. 卸载和清理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#8-卸载和清理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>如果要撤销安装,可以使用命令 <code>make uninstall</code>。但是,这不会删除任何创建的目录。</p>
<p>安装后,你可以通过从源代码树中删除构建文件来释放磁盘空间,命令是 <code>make clean</code>。这将保留 <code>configure</code> 程序创建的文件,以便你稍后可以使用 <code>make</code> 重新构建所有内容。要将源代码树重置为分发时的状态,请使用 <code>make distclean</code></p>
<h3 id="二进制安装">二进制安装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#二进制安装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="1-下载">1. 下载<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#1-下载" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>在开始二进制安装之前,你需要首先下载适用于你的操作系统的 PostgreSQL 二进制包。你可以从 PostgreSQL 官方网站或者你的操作系统的软件库中获取它。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 通过 wget 命令下载</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">wget</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 通过 curl 命令下载</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">curl</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -O</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm</span></span></code></pre></figure>
<h4 id="2-安装">2. 安装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#2-安装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>安装二进制包通常比源码安装更快更简单。根据你的操作系统和包管理器,这里介绍几种常用的安装方法:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 在 Red Hat/CentOS 中安装</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> rpm</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -ivh</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> pgdg-redhat-repo-latest.noarch.rpm</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> yum</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgresql-server</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 在 Debian/Ubuntu 中安装</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt-get</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt-get</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgresql</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 在 Debian/Ubuntu 中使用自定义选项进行安装</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgresql</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --prefix=/usr/local/pgsql</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --datadir=/usr/local/pgsql/data</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 在 Red Hat/CentOS 中使用自定义选项进行安装</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> yum</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgresql-server</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --prefix=/usr/local/pgsql</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --datadir=/usr/local/pgsql/data</span></span></code></pre></figure>
<p>安装过程中,你可以使用以下选项来自定义安装:</p>
<div class="table-container"><table><thead><tr><th>选项</th><th>描述</th></tr></thead><tbody><tr><td><code>--prefix=PREFIX</code></td><td>定义安装的根目录,所有文件和目录都将放在这个目录下</td></tr><tr><td><code>--exec-prefix=EPREFIX</code></td><td>定义架构相关文件的安装目录,通常与 <code>--prefix</code> 相同</td></tr><tr><td><code>--bindir=DIR</code></td><td>定义用户可执行文件(如 psql的安装目录</td></tr><tr><td><code>--datadir=DIR</code></td><td>定义只读架构独立数据的安装目录</td></tr><tr><td><code>--libdir=DIR</code></td><td>定义库文件的安装目录</td></tr><tr><td><code>--sysconfdir=DIR</code></td><td>定义系统级配置文件的安装目录</td></tr><tr><td><code>--sharedstatedir=DIR</code></td><td>定义可修改的单主机数据的安装目录</td></tr><tr><td><code>--localstatedir=DIR</code></td><td>定义可修改的多主机数据的安装目录</td></tr><tr><td><code>--libexecdir=DIR</code></td><td>定义可执行程序和其他程序运行的安装目录</td></tr><tr><td><code>--includedir=DIR</code></td><td>定义 C 头文件的安装目录</td></tr><tr><td><code>--mandir=DIR</code></td><td>定义 man 文档的安装目录</td></tr><tr><td><code>--docdir=DIR</code></td><td>定义文档的根目录</td></tr></tbody></table></div>
<h4 id="3-验证">3. 验证<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#3-验证" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>安装完成后,验证 PostgreSQL 是否已成功安装和配置。可以通过以下命令来验证安装:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 检查 PostgreSQL 服务状态</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> systemctl</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> status</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgresql</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用 psql 命令行工具连接到 PostgreSQL 服务器</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">psql</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -U</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 显示 PostgreSQL 服务器的版本信息</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">psql</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -U</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -c</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'SELECT version();'</span></span></code></pre></figure>
<p>二进制安装成功后,可以直接使用,默认不需要后续的配置。</p>
<h2 id="设置">设置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#设置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="1-共享库设置-shared-libraries">1. 共享库设置 (Shared Libraries)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#1-共享库设置-shared-libraries" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>在某些系统中,你需要告诉系统如何找到新安装的共享库。这不包括 FreeBSD, HP-UX, Linux, NetBSD, OpenBSD 和 Solaris 等系统。设置共享库搜索路径的方法因平台而异,但最常用的方法是设置环境变量 <code>LD_LIBRARY_PATH</code>。以下是如何在 Bourne shell (如 sh, ksh, bash, zsh) 中设置:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">LD_LIBRARY_PATH</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/usr/local/pgsql/lib</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">export</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> LD_LIBRARY_PATH</span></span></code></pre></figure>
<h3 id="2-环境变量设置-environment-variables">2. 环境变量设置 (Environment Variables)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#2-环境变量设置-environment-variables" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>如果你将 PostgreSQL 安装到 <code>/usr/local/pgsql</code> 或其他默认情况下不搜索程序的位置,你应该将 <code>/usr/local/pgsql/bin</code>(或你在第一步中设置的 <code>--bindir</code> 选项)添加到你的 <code>PATH</code> 中。这样可以使 PostgreSQL 的使用更加方便。以下是如何在 Bourne shell 中设置:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">PATH</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/usr/local/pgsql/bin:</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$PATH</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">export</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PATH</span></span></code></pre></figure>
<p>为了让你的系统找到 man 文档,你需要将以下行添加到一个 shell 启动文件中,除非你安装到了默认搜索的位置:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">MANPATH</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/usr/local/pgsql/share/man:</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$MANPATH</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">export</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> MANPATH</span></span></code></pre></figure>
<p>环境变量 <code>PGHOST</code><code>PGPORT</code> 指定了数据库服务器的主机和端口,覆盖了编译时的默认设置。如果你打算远程运行客户端应用程序,那么每个计划使用数据库的用户都应设置 <code>PGHOST</code>。这不是必需的,但可以通过大多数客户端程序的命令行选项来传递设置。</p>
<h3 id="3-配置脚本示例">3. 配置脚本示例<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#3-配置脚本示例" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#!/bin/bash</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 将配置写入 ~/.bash_profile 文件</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">cat</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> >></span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ~/.bash_profile</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;&lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">EOF</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">export PGDATA=/opt/postgres/data</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">export PGHOME=/opt/postgres/pgsql</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">export LD_LIBRARY_PATH=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$pg_home</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$LD_LIBRARY_PATH</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">export PATH=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$pg_home</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/bin:</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$PATH</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">:.</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">export MANPATH=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$pg_home</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/share/man:</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$MANPATH</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">export PGHOST=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$PGDATA</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">export PGUSER=postgres</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">export PGDATABASE=postgres</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">EOF</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 重新加载 ~/.bash_profile</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">source</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ~/.bash_profile</span></span></code></pre></figure>
<h2 id="使用">使用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="psql-命令">Psql 命令<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#psql-命令" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<div class="table-container"><table><thead><tr><th>内置命令</th><th>作用描述</th></tr></thead><tbody><tr><td><code>\q</code></td><td>退出 psql</td></tr><tr><td><code>\l</code><code>\list</code></td><td>列出所有数据库</td></tr><tr><td><code>\c [数据库名]</code></td><td>连接到新数据库</td></tr><tr><td><code>\dt</code></td><td>列出当前数据库中的所有表</td></tr><tr><td><code>\d [表名]</code></td><td>显示表的结构(列、数据类型、权限等)</td></tr><tr><td><code>\du</code></td><td>列出所有 PostgreSQL 用户角色</td></tr><tr><td><code>\e</code></td><td>打开文本编辑器</td></tr><tr><td><code>\a</code></td><td>切换输出格式(对齐或非对齐)</td></tr><tr><td><code>\x</code></td><td>切换扩展显示模式</td></tr><tr><td><code>\timing</code></td><td>显示 SQL 查询的执行时间</td></tr><tr><td><code>\i [文件名]</code></td><td>执行文件中的 SQL 命令</td></tr><tr><td><code>\o [文件名]</code></td><td>将查询结果输出到文件</td></tr><tr><td><code>\s</code></td><td>显示命令历史</td></tr><tr><td><code>\?</code></td><td>显示所有 psql 命令的帮助信息</td></tr><tr><td><code>\h [SQL命令]</code></td><td>显示 SQL 命令的语法和选项</td></tr><tr><td><code>\set</code></td><td>列出所有 psql 变量及其值</td></tr><tr><td><code>\unset</code></td><td>删除一个或多个 psql 变量</td></tr><tr><td><code>\g</code><code>;</code></td><td>执行查询</td></tr><tr><td><code>\qecho [文本]</code></td><td>在输出中显示文本(通常用于脚本中添加注释或空行)</td></tr><tr><td><code>\conninfo</code></td><td>显示当前数据库连接的详细信息</td></tr></tbody></table></div>
<h3 id="1-初始化数据库目录">1. 初始化数据库目录<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#1-初始化数据库目录" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>首先,需要使用 <code>initdb</code> 命令来初始化数据库目录。该命令会创建必要的目录结构和系统目录,以便 PostgreSQL 数据库能够运行。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># -D 选项指定数据库目录的路径</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">initdb</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -D</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /path/to/data/directory</span></span></code></pre></figure>
<h3 id="2-配置文件">2. 配置文件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#2-配置文件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="postgresqlconf">postgresql.conf<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#postgresqlconf" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>PostgreSQL 的主配置文件,可以在这里设置各种参数,如内存使用、连接数等。下面是常用配置:</p>
<div class="table-container"><table><thead><tr><th>配置选项</th><th>作用</th><th>示例配置</th></tr></thead><tbody><tr><td><strong>连接设置</strong></td><td></td><td></td></tr><tr><td><code>listen_addresses</code></td><td>定义服务器监听的地址</td><td><code>listen_addresses = '*'</code></td></tr><tr><td><code>port</code></td><td>定义服务器监听的端口</td><td><code>port = 5432</code></td></tr><tr><td><code>max_connections</code></td><td>最大并发连接数</td><td><code>max_connections = 100</code></td></tr><tr><td><code>superuser_reserved_connections</code></td><td>为超级用户保留的连接数</td><td><code>superuser_reserved_connections = 3</code></td></tr><tr><td><code>unix_socket_directories</code></td><td>Unix 域套接字的目录</td><td><code>unix_socket_directories = '/var/run/postgresql'</code></td></tr><tr><td><strong>认证和安全</strong></td><td></td><td></td></tr><tr><td><code>ssl</code></td><td>是否启用 SSL</td><td><code>ssl = on</code></td></tr><tr><td><code>ssl_cert_file</code></td><td>SSL 证书文件的位置</td><td><code>ssl_cert_file = '/path/to/server.crt'</code></td></tr><tr><td><code>ssl_key_file</code></td><td>SSL 私钥文件的位置</td><td><code>ssl_key_file = '/path/to/server.key'</code></td></tr><tr><td><code>password_encryption</code></td><td>密码加密方式</td><td><code>password_encryption = scram-sha-256</code></td></tr><tr><td><code>hba_file</code></td><td><code>pg_hba.conf</code> 文件的位置</td><td><code>hba_file = '/path/to/pg_hba.conf'</code></td></tr><tr><td><strong>内存使用</strong></td><td></td><td></td></tr><tr><td><code>shared_buffers</code></td><td>用于缓存的内存大小</td><td><code>shared_buffers = 128MB</code></td></tr><tr><td><code>work_mem</code></td><td>查询操作可以使用的内存量</td><td><code>work_mem = 4MB</code></td></tr><tr><td><code>maintenance_work_mem</code></td><td>维护操作(如 VACUUM可以使用的内存量</td><td><code>maintenance_work_mem = 64MB</code></td></tr><tr><td><code>effective_cache_size</code></td><td>估计的操作系统缓存大小</td><td><code>effective_cache_size = 512MB</code></td></tr><tr><td><strong>查询调优</strong></td><td></td><td></td></tr><tr><td><code>seq_page_cost</code></td><td>顺序磁盘页的成本</td><td><code>seq_page_cost = 1.0</code></td></tr><tr><td><code>random_page_cost</code></td><td>随机磁盘页的成本</td><td><code>random_page_cost = 4.0</code></td></tr><tr><td><code>cpu_tuple_cost</code></td><td>处理每个元组的 CPU 成本</td><td><code>cpu_tuple_cost = 0.01</code></td></tr><tr><td><code>cpu_index_tuple_cost</code></td><td>处理每个索引元组的 CPU 成本</td><td><code>cpu_index_tuple_cost = 0.005</code></td></tr><tr><td><code>cpu_operator_cost</code></td><td>执行操作符的 CPU 成本</td><td><code>cpu_operator_cost = 0.0025</code></td></tr><tr><td><strong>日志记录</strong></td><td></td><td></td></tr><tr><td><code>logging_collector</code></td><td>是否启用日志收集</td><td><code>logging_collector = on</code></td></tr><tr><td><code>log_directory</code></td><td>日志文件的存储目录</td><td><code>log_directory = 'pg_log'</code></td></tr><tr><td><code>log_filename</code></td><td>日志文件的命名方式</td><td><code>log_filename = 'postgresql.log'</code></td></tr><tr><td><code>log_statement</code></td><td>记录的 SQL 语句类型(例如所有语句或只有 DDL</td><td><code>log_statement = 'all'</code></td></tr><tr><td><code>log_duration</code></td><td>是否记录语句的执行时间</td><td><code>log_duration = on</code></td></tr><tr><td><code>log_min_duration_statement</code></td><td>记录执行时间超过指定毫秒数的语句</td><td><code>log_min_duration_statement = 500</code></td></tr><tr><td><strong>运行时统计</strong></td><td></td><td></td></tr><tr><td><code>track_activities</code></td><td>是否跟踪服务器进程的活动</td><td><code>track_activities = on</code></td></tr><tr><td><code>track_counts</code></td><td>是否跟踪访问统计信息</td><td><code>track_
<h4 id="pg_hbaconf">pg_hba.conf<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#pg_hbaconf" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>pg_hba.conf</code> 文件用于控制哪些客户端可以连接到数据库,以及它们可以使用的认证方法。以下是 <code>pg_hba.conf</code> 中的常见配置项、作用和示例配置:</p>
<div class="table-container"><table><thead><tr><th>类型</th><th>数据库</th><th>用户</th><th>地址</th><th>认证方法</th><th>说明</th></tr></thead><tbody><tr><td><code>local</code></td><td><code>all</code></td><td><code>all</code></td><td></td><td><code>trust</code></td><td>本地所有用户对所有数据库的连接不需要密码</td></tr><tr><td><code>host</code></td><td><code>all</code></td><td><code>all</code></td><td><code>127.0.0.1/32</code></td><td><code>md5</code></td><td>从本地通过 TCP/IP 连接的所有用户需要密码</td></tr><tr><td><code>host</code></td><td><code>mydb</code></td><td><code>myuser</code></td><td><code>192.168.1.0/24</code></td><td><code>md5</code></td><td>从指定网络连接到指定数据库的指定用户需要密码</td></tr><tr><td><code>hostssl</code></td><td><code>all</code></td><td><code>all</code></td><td><code>0.0.0.0/0</code></td><td><code>md5</code></td><td>从任何地址通过 SSL 连接的所有用户需要密码</td></tr><tr><td><code>hostnossl</code></td><td><code>all</code></td><td><code>all</code></td><td><code>0.0.0.0/0</code></td><td><code>reject</code></td><td>拒绝非 SSL 连接</td></tr></tbody></table></div>
<ul>
<li><strong>类型</strong>:连接类型。常见的有 <code>local</code>Unix 域套接字)、<code>host</code>TCP/IP 套接字)、<code>hostssl</code>(仅限 SSL 的 TCP/IP 连接)和 <code>hostnossl</code>(不使用 SSL 的 TCP/IP 连接)。</li>
<li><strong>数据库</strong>:适用的数据库名。<code>all</code> 表示所有数据库。</li>
<li><strong>用户</strong>:适用的用户名。<code>all</code> 表示所有用户。</li>
<li><strong>地址</strong>:客户端的 IP 地址。对于 <code>local</code> 类型,这一列是空的。对于 <code>host</code> 类型,这是一个 CIDR 地址或一个 IP 地址和一个掩码。</li>
<li><strong>认证方法</strong>:常见的有 <code>trust</code>(不需要密码)、<code>reject</code>(拒绝连接)、<code>md5</code>(密码认证)、<code>password</code>(明文密码)等。</li>
</ul>
<p><code>pg_hba.conf</code> 文件的每一行都是一个连接规则PostgreSQL 会按照文件中的顺序评估这些规则,直到找到一个匹配的规则为止。因此,顺序很重要,特别是当有多个规则可能匹配同一个连接时。</p>
<h3 id="3-启动数据库">3. 启动数据库<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#3-启动数据库" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>一旦数据库目录被成功初始化,可以使用 <code>pg_ctl</code> 命令来启动 PostgreSQL 数据库。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># -D 选项指定了数据库目录的路径。</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pg_ctl</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> start</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -D</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /path/to/data/directory</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 或者</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">postgres</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -D</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /path/to/data/directory</span></span></code></pre></figure>
<h3 id="4-验证数据库是否正在运行">4. 验证数据库是否正在运行<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#4-验证数据库是否正在运行" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>为了验证 PostgreSQL 数据库是否正在运行,可以使用 <code>psql</code> 命令连接到数据库并执行一个简单的查询,例如:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">psql</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -U</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -c</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;SELECT version();&quot;</span></span></code></pre></figure>
<h3 id="5-配置-systemctl-服务">5. 配置 Systemctl 服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#5-配置-systemctl-服务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<ol>
<li>关闭 postgresql 数据库。</li>
<li>创建一个 Systemd 服务文件,通常位于 <code>/etc/systemd/system/postgresql.service</code></li>
<li>添加以下内容(需要结合配置脚本示例)。</li>
</ol>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[Unit]</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Description</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">PostgreSQL</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> database</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> server</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Documentation</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">man:postgres</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">After</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">network.target</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[Service]</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Type</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">forking</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">User</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">postgres</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Group</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">postgres</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Environment</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">PGPORT</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">5432</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Environment</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">PGDATA</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/opt/postgresql/data</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">OOMScoreAdjust</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">-1000</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ExecStart</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/opt/postgresql/pgsql/bin/pg_ctl</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> start</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -D</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ${PGDATA} </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">-s</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;-p ${</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">PGPORT</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">}&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -w</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -t</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 300</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ExecStop</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/opt/postgresql/pgsql/bin/pg_ctl</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> stop</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -D</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ${PGDATA} </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">-s</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -m</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> fast</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ExecReload</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/opt/postgresql/pgsql//bin/pg_ctl</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> reload</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -D</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ${PGDATA} </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">-s</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">KillMode</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mixed</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">KillSignal</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">SIGINT</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">TimeoutSec</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">30</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[Install]</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">WantedBy</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">multi-user.target</span></span></code></pre></figure>
<ol>
<li>重新加载 Systemd 配置:<code>sudo systemctl daemon-reload</code></li>
<li>启动服务:<code>sudo systemctl start postgresql</code></li>
<li>设置开机启动:<code>sudo systemctl enable postgresql</code></li>
</ol>
<h3 id="6-创建用户和数据库">6. 创建用户和数据库<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#6-创建用户和数据库" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建一个新用户</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -u</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> createuser</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -P</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">usernam</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建一个新数据库</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -u</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> createdb</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -O</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">usernam</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">dbnam</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 为用户分配特定权限</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -u</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> psql</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -c</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;GRANT ALL PRIVILEGES ON DATABASE &lt;dbname> TO &lt;username>;&quot;</span></span></code></pre></figure>
<h3 id="7-用户和角色管理">7. 用户和角色管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#7-用户和角色管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建角色</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -u</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> createuser</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">role_nam</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 为角色分配权限</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -u</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> psql</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -c</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;GRANT &lt;permission> ON &lt;object> TO &lt;role_name>;&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除角色</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -u</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dropuser</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">role_nam</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置密码策略和验证</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -u</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> postgres</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> psql</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -c</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;ALTER USER &lt;username> WITH PASSWORD '&lt;password>';&quot;</span></span></code></pre></figure>
<h3 id="8-备份和恢复">8. 备份和恢复<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#8-备份和恢复" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>备份和恢复是数据库管理的关键部分确保数据的安全和完整性。PostgreSQL 提供了 <code>pg_dump</code><code>pg_restore</code> 工具来帮助管理员进行这些操作。</p>
<h4 id="pg_dump-备份"><code>pg_dump</code> 备份<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#pg_dump-备份" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>pg_dump</code> 是用于备份 PostgreSQL 数据库的工具。它可以生成包含数据库内容的 SQL 脚本文件或其他格式,如 tar 和 custom。使用以下命令进行全量备份</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pg_dump</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -U</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">usernam</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -h</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">hos</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">t</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -p</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">por</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">t</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -F</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> c</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -b</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">dbnam</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ></span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> backup.dump</span></span></code></pre></figure>
<p>额外的选项包括 <code>--data-only</code><code>--schema-only</code>,这些选项可以让用户选择备份数据或者仅备份架构。</p>
<p>使用 <code>pg_restore</code> 来验证备份文件的完整性。即使你不打算立即恢复,也应该定期验证备份。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pg_restore</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --list</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> backup.dump</span></span></code></pre></figure>
<h4 id="pg_restore-恢复"><code>pg_restore</code> 恢复<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#pg_restore-恢复" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>pg_restore</code> 是用于恢复由 <code>pg_dump</code> 创建的备份的工具。使用以下命令进行恢复:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pg_restore</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -U</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">usernam</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -h</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">hos</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">t</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -p</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">por</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">t</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -d</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">dbnam</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> backup.dump</span></span></code></pre></figure>
<p><strong>注意</strong></p>
<ul>
<li>在恢复之前,确保目标数据库是空的或你已经备份了任何重要的数据。</li>
<li>如果备份是在不同版本的 PostgreSQL 上创建的,可能会遇到兼容性问题。在这种情况下,确保先测试恢复过程。</li>
<li>如果你正在恢复到一个活动的数据库,你可能需要在恢复前停止数据库服务。</li>
</ul>
<h2 id="更新">更新<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#更新" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="小版本升级minor-version-upgrade">小版本升级Minor Version Upgrade<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#小版本升级minor-version-upgrade" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>**小版本升级通常涉及到安全修复和错误修复,而不会改变数据库的内部格式。**这些升级通常比较简单,并且可以通过替换二进制文件来完成。如下是 PostgreSQL 15.3 升级到 15.4 的小版本升级示例:</p>
<p><strong>步骤</strong>:</p>
<ol>
<li>
<p><strong>备份数据</strong>:</p>
<p>在进行任何升级之前,始终确保备份您的数据库。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pg_dumpall</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ></span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> backup.sql</span></span></code></pre></figure>
</li>
<li>
<p><strong>停止 PostgreSQL 服务</strong>:</p>
<p>使用 <code>pg_ctl stop</code> 或您的系统的服务管理工具停止 PostgreSQL 服务。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pg_ctl</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> stop</span></span></code></pre></figure>
</li>
<li>
<p><strong>替换二进制文件</strong>:</p>
<p>下载并安装 PostgreSQL 15.4 的二进制文件。</p>
</li>
<li>
<p><strong>重启 PostgreSQL 服务</strong>:</p>
<p>使用 <code>pg_ctl start</code> 或您的系统的服务管理工具重新启动 PostgreSQL 服务。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pg_ctl</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> start</span></span></code></pre></figure>
</li>
<li>
<p><strong>检查日志和运行测试</strong>:</p>
<p>检查 <code>/usr/local/pgsql/data/pg_log/</code> 中的日志文件以确保没有错误,并运行任何必要的测试以验证系统的功能。</p>
</li>
</ol>
<h3 id="跨版本升级major-version-upgrade">跨版本升级Major Version Upgrade<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#跨版本升级major-version-upgrade" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>跨版本升级涉及到从一个主版本升级到另一个主版本,例如从 PostgreSQL 15 升级到 PostgreSQL 16。**这些升级通常会涉及到内部格式的变化,因此需要更多的注意和准备。**如下是 PostgreSQL 15.4 升级到 16.0 的大版本升级示例:</p>
<p><strong>步骤</strong>:</p>
<ol>
<li>
<p><strong>备份数据</strong>:</p>
<p>在进行任何升级之前,始终确保备份您的数据库。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pg_dumpall</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ></span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> backup.sql</span></span></code></pre></figure>
</li>
<li>
<p><strong>检查兼容性</strong>:</p>
<p>检查新版本的 PostgreSQL 文档以确保您的应用程序和数据库架构与新版本兼容。</p>
</li>
<li>
<p><strong>安装新版本的 PostgreSQL</strong>:</p>
<p>在同一系统上安装新版本的 PostgreSQL但不要覆盖旧版本。</p>
</li>
<li>
<p><strong>使用 pg_upgrade 或逻辑备份和恢复</strong>:</p>
<p>使用 <code>pg_upgrade</code> 工具快速升级。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pg_upgrade</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">--old-datadir </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;/usr/local/pgsql/15.4/data&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">--new-datadir </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;/usr/local/pgsql/16.0/data&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">--old-bindir </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;/usr/local/pgsql/15.4/bin&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">--new-bindir </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;/usr/local/pgsql/16.0/bin&quot;</span></span></code></pre></figure>
</li>
<li>
<p><strong>测试新集群</strong>:</p>
<p>在新集群上运行测试以确保它按预期工作。</p>
</li>
<li>
<p><strong>切换到新集群</strong>:</p>
<p>一旦满意,可以切换到新集群并更新任何连接字符串和配置。</p>
</li>
<li>
<p><strong>监控性能和错误</strong>:</p>
<p>在升级后监控系统性能和错误,以确保一切正常。</p>
</li>
<li>
<p><strong>(可选)删除旧集群</strong>:</p>
<p>确保新集群运行正常后,可以选择删除旧集群以释放空间。</p>
</li>
</ol></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:1,&quot;scale&quot;:1.1,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:-1,&quot;scale&quot;:0.9,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#序言" data-for="序言">序言</a></li><li class="depth-0"><a href="#依赖" data-for="依赖">依赖</a></li><li class="depth-1"><a href="#基本依赖" data-for="基本依赖">基本依赖</a></li><li class="depth-1"><a href="#拓展依赖" data-for="拓展依赖">拓展依赖</a></li><li class="depth-1"><a href="#依赖检查" data-for="依赖检查">依赖检查</a></li><li class="depth-0"><a href="#安装" data-for="安装">安装</a></li><li class="depth-1"><a href="#源码安装" data-for="源码安装">源码安装</a></li><li class="depth-1"><a href="#二进制安装" data-for="二进制安装">二进制安装</a></li><li class="depth-0"><a href="#设置" data-for="设置">设置</a></li><li class="depth-1"><a href="#1-共享库设置-shared-libraries" data-for="1-共享库设置-shared-libraries">1. 共享库设置 (Shared Libraries)</a></li><li class="depth-1"><a href="#2-环境变量设置-environment-variables" data-for="2-环境变量设置-environment-variables">2. 环境变量设置 (Environment Variables)</a></li><li class="depth-1"><a href="#3-配置脚本示例" data-for="3-配置脚本示例">3. 配置脚本示例</a></li><li class="depth-0"><a href="#使用" data-for="使用">使用</a></li><li class="depth-1"><a href="#psql-命令" data-for="psql-命令">Psql 命令</a></li><li class="depth-1"><a href="#1-初始化数据库目录" data-for="1-初始化数据库目录">1. 初始化数据库目录</a></li><li class="depth-1"><a href="#2-配置文件" data-for="2-配置文件">2. 配置文件</a></li><li class="depth-1"><a href="#3-启动数据库" data-for="3-启动数据库">3. 启动数据库</a></li><li class="depth-1"><a href="#4-验证数据库是否正在运行" data-for="4-验证数据库是否正在运行">4. 验证数据库是否正在运行</a></li><li class="depth-1"><a href="#5-配置-systemctl-服务" data-for="5-配置-systemctl-服务">5. 配置 Systemctl 服务</a></li><li class="depth-1"><a href="#6-创建用户和数据库" data-for="6-创建用户和数据库">6. 创建用户和数据库</a></li><li class="depth-1"><a href="#7-用户和角色管理" data-for="7-用户和角色管理">7. 用户和角色管理</a></li><li class="depth-1"><a href="#8-备份和恢复" data-for="8-备份和恢复">8. 备份和恢复</a></li><li class="depth-0"><a href="#更新" data-for="更新">更新</a></li><li class="depth-1"><a href="#小版本升级minor-version-upgrade" data-for="小版本升级minor-version-upgrade">小版本升级Minor Version Upgrade</a></li><li class="depth-1"><a href="#跨版本升级major-version-upgrade" data-for="跨版本升级major-version-upgrade">跨版本升级Major Version Upgrade</a></li></ul></div></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{&quot;path&quot;:&quot;Personal&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2018&quot;,&quot;colla
</script><script type="module">
let mermaidImport = undefined
document.addEventListener('nav', async () => {
if (document.querySelector("code.mermaid")) {
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
const mermaid = mermaidImport.default
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
})
await mermaid.run({
querySelector: '.mermaid'
})
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../postscript.js" type="module"></script></html>