# PG 入门

> 从安装服务器、编写 SQL 到连接应用，一步步认识 PostgreSQL，并探索下一阶段的学习方向。

---

LLMS 索引： [llms.txt](/llms.txt)

---

**Pavel Luzanov, Egor Rogov & Igor Levshin** · 第 11 版 · PostgreSQL 17 · 2025 年

[开始阅读](/introduction/) · [整书打印](/_print/) · [英文原版 PDF](/downloads/introbook_v11_en.pdf) · [勘误与阅读说明](#errata) · [English](/en/)

## 目录 {#contents}

- [引言](/introduction/)
- [1 关于 PostgreSQL](/01-about-postgresql/)
- [2 PostgreSQL 17 新特性](/02-what-s-new-in-postgresql-17/)
- [3 安装与快速入门](/03-installation-and-quick-start/)
- [4 体验 SQL](/04-trying-sql/)
- [5 演示数据库](/05-demo-database/)
- [6 应用中的 PostgreSQL](/06-postgresql-for-applications/)
- [7 配置 PostgreSQL](/07-configuring-postgresql/)
- [8 pgAdmin](/08-pgadmin/)
- [9 其他特性](/09-additional-features/)
- [10 教育与认证](/10-education-and-certification/)
- [11 黑客银河漫游指南](/11-the-hacker-s-guide-to-the-galaxy/)
- [12 Postgres Professional](/12-postgres-professional/)
- [出版信息与致谢](/publication/)

## 插图 {#figures}

- [图 3.1](/03-installation-and-quick-start/#intro-fig-3-1) — 选择安装组件
- [图 3.2](/03-installation-and-quick-start/#intro-fig-3-2) — 选择数据目录
- [图 3.3](/03-installation-and-quick-start/#intro-fig-3-3) — 设置服务器连接选项
- [图 3.4](/03-installation-and-quick-start/#intro-fig-3-4) — 选择服务器的默认配置
- [图 4.1](/04-trying-sql/#intro-fig-4-1) — 从开始菜单启动 SQL Shell
- [图 5.1](/05-demo-database/#intro-fig-5-1) — 订票数据库中的关系
- [图 5.2](/05-demo-database/#intro-fig-5-2) — 演示数据库中的机场与航线
- [图 7.1](/07-configuring-postgresql/#intro-fig-7-1) — 将 1C 信息库连接到 PostgreSQL
- [图 8.1](/08-pgadmin/#intro-fig-8-1) — 在 pgAdmin 中注册服务器
- [图 8.2](/08-pgadmin/#intro-fig-8-2) — 在 pgAdmin 中浏览数据库对象
- [图 8.3](/08-pgadmin/#intro-fig-8-3) — 在 pgAdmin 中运行查询
- [图 10.1](/10-education-and-certification/#intro-fig-10-1) — 管理课程路径
- [图 10.2](/10-education-and-certification/#intro-fig-10-2) — 开发课程路径
- [图 10.3](/10-education-and-certification/#intro-fig-10-3) — Postgres Pro Enterprise 课程先修要求
- [图 10.4](/10-education-and-certification/#intro-fig-10-4) — PostgreSQL：SQL 基础——书籍封面
- [图 10.5](/10-education-and-certification/#intro-fig-10-5) — Evgeny Morgunov
- [图 10.6](/10-education-and-certification/#intro-fig-10-6) — 数据库技术基础——书籍封面
- [图 10.7](/10-education-and-certification/#intro-fig-10-7) — Boris Novikov
- [图 10.8](/10-education-and-certification/#intro-fig-10-8) — PostgreSQL 14 内部原理——书籍封面
- [图 10.9](/10-education-and-certification/#intro-fig-10-9) — Egor Rogov
- [图 10.10](/10-education-and-certification/#intro-fig-10-10) — PostgreSQL 监控——书籍封面
- [图 10.11](/10-education-and-certification/#intro-fig-10-11) — Alexey Lesovsky
- [图 10.12](/10-education-and-certification/#intro-fig-10-12) — 数据库指南——书籍封面
- [图 10.13](/10-education-and-certification/#intro-fig-10-13) — Vladimir Komarov

## 关于本版 {#edition}

© Postgres Professional, 2016–2025。本中文译稿由 [2025 年第 11 版 · PostgreSQL 17 英文 PDF](/downloads/introbook_v11_en.pdf) 译出，沿用原版插图。原书名为 Postgres. The First Experience，英文 Web 版标题使用 Postgres: The First Experience；本站沿用中文书名《PostgreSQL 入门》。本 Web 版保留原书的章节顺序、示例和特定版本行为，原书页码引用可跳转至相应章节位置。书中的版本、课程、活动和公司信息均按原版时点保留，第 10–12 章的“我们”指原作者及 Postgres Professional。中文版本由冯若航（Vonng）翻译与审校。

[出版方与原版](https://postgrespro.com/community/books/introbook) · [出版信息与致谢](/publication/)

## 勘误与阅读说明 {#errata}

以下九条说明译自英文 Web 版；该版本在对照所提供的 PDF 与 PostgreSQL 17 文档后补充了这些说明。中文正文按原版含义翻译，代码和输出保留原样。章节摘要与描述性图注也是 Web 版为方便阅读而添加的内容。下列页码均指原书印刷页码，PDF 页序比它大 1。

### 复制槽状态——第 18 页 {#errata-replication-view}

在[逻辑复制](/02-what-s-new-in-postgresql-17/#intro-page-19)一节中，`conflicting` 和 `invalidation_reason` 属于 `pg_replication_slots`。原文误写为 `pg_stat_replication`。参见 [PostgreSQL 17 视图定义](https://www.postgresql.org/docs/17/view-pg-replication-slots.html)。

### 直接 TLS——第 20 页 {#errata-direct-tls}

在[访问控制](/02-what-s-new-in-postgresql-17/#intro-page-21)一节中，应将 `sslnegotiation=direct` 与 `sslmode=require`、`verify-ca` 或 `verify-full` 配合使用。原文中的 `requiredirect` 不是有效值。参见 [libpq 连接参数](https://www.postgresql.org/docs/17/libpq-connect.html#LIBPQ-CONNECT-SSLNEGOTIATION)。

### 分组键——第 20 页 {#errata-grouping}

在[查询规划与执行](/02-what-s-new-in-postgresql-17/#intro-section-2-8)一节中，查询规划器为了利用已有顺序，会重新排列 `GROUP BY` 的键，而不是表。参见 [enable_group_by_reordering](https://www.postgresql.org/docs/17/runtime-config-query.html#GUC-ENABLE-GROUP-BY-REORDERING)。

### 并行函数——第 20 页 {#errata-parallel}

同一节还需要补充一个条件：使用子事务进行错误恢复的函数，在其所有操作都满足安全要求时，可以显式标记为 `PARALLEL SAFE`。函数默认仍是 `PARALLEL UNSAFE`；包含 `EXCEPTION` 块并不会自动使函数具备并行安全性。参见 [CREATE FUNCTION](https://www.postgresql.org/docs/17/sql-createfunction.html)。

### 同步选项——第 23 页 {#errata-sync-method}

在[其他](/02-what-s-new-in-postgresql-17/#intro-section-2-13)一节中，工具的选项名为 `--sync-method`，原文误写为 `--synch-method`。参见 [PostgreSQL 17 发行说明](https://www.postgresql.org/docs/17/release-17.html)。

### 合并 I/O 大小——第 23 页 {#errata-io-units}

该节中的 `io_combine_limit` 在未指定单位时，以数据库块为单位，通常每块为 8 kB；它并非直接表示字节数。默认值为 128 kB。参见 [io_combine_limit](https://www.postgresql.org/docs/17/runtime-config-resource.html#GUC-IO-COMBINE-LIMIT)。

### 升级时的逻辑复制——第 17 页 {#errata-upgrade}

[升级说明](/02-what-s-new-in-postgresql-17/#intro-section-2-4)适用于旧数据库集簇为 PostgreSQL 17 或更高版本的情况。它并不保证从 17 之前的版本升级到 17 时会保留逻辑复制槽和订阅。参见 [PostgreSQL 17 发行说明](https://www.postgresql.org/docs/17/release-17.html)。

### Debian 和 Ubuntu 安装——第 30 页 {#errata-installation}

[软件仓库命令](/03-installation-and-quick-start/#intro-subsection-3-2-1)需要一个空格：`$(lsb_release -cs)`。PDF 中印成了 `$(lsb_release-cs)`，这会尝试执行另一个命令。发行版列表和 `apt-key` 流程也反映了原版的情况。新安装时，请遵循 PostgreSQL 项目当前的 [Debian](https://www.postgresql.org/download/linux/debian/) 或 [Ubuntu](https://www.postgresql.org/download/linux/ubuntu/) 安装说明，并选择 PostgreSQL 17，以与本书保持一致。

### SELECT 文档链接——第 53 页 {#errata-select-link}

[分组](/04-trying-sql/#intro-section-4-9)一节末尾的原始链接在英文 Web 版审校时返回了 HTTP 404。显示的地址保留原文写法，但链接现在指向 [PostgreSQL 17 GROUP BY 参考文档](https://www.postgresql.org/docs/17/sql-select.html#SQL-GROUPBY)。原始目标已记录在来源清单中。

---

本节页面：

- [引言](/introduction/): 第 11 版十二章导读：从安装 PostgreSQL 17，到 SQL、应用程序与进阶学习。
- [关于 PostgreSQL](/01-about-postgresql/): PostgreSQL 的历史、许可、可靠性、SQL 支持、可扩展性，以及数据库背后的社区。
- [PostgreSQL 17 新特性](/02-what-s-new-in-postgresql-17/): PostgreSQL 17 的主要更新，包括 SQL/JSON、复制、清理、增量备份、查询规划和监控。
- [安装与快速入门](/03-installation-and-quick-start/): 在 Windows、Debian 或 Ubuntu 上安装 PostgreSQL 17，管理服务并找到配置文件。
- [体验 SQL](/04-trying-sql/): 使用 psql 连接 PostgreSQL，创建表、查询和修改数据、组合关系，并尝试事务。
- [演示数据库](/05-demo-database/): 探索航空订票演示数据库及其表和视图，使用贴近实际的航班数据编写 SQL 查询。
- [应用中的 PostgreSQL](/06-postgresql-for-applications/): 使用 PHP、Perl、Python 和 Java 将应用程序连接到 PostgreSQL，了解查询和事务处理示例。
- [配置 PostgreSQL](/07-configuring-postgresql/): 为首次部署配置 PostgreSQL 17，涵盖内存、日志、连接设置和访问规则。
- [pgAdmin](/08-pgadmin/): 在 pgAdmin 中注册 PostgreSQL 服务器、浏览数据库对象，并在图形化查询工具中运行 SQL。
- [其他特性](/09-additional-features/): 通过完整的 SQL 示例，体验 PostgreSQL 的全文检索、JSON、JSONB 和外部数据包装器。
- [教育与认证](/10-education-and-certification/): 文档、培训课程、认证、学术项目，以及 原版 2025 年版本列出的书籍。
- [黑客银河漫游指南](/11-the-hacker-s-guide-to-the-galaxy/): 通过博客、邮件列表、Commitfest、会议与社区讨论，关注 PostgreSQL 动态。
- [Postgres Professional](/12-postgres-professional/): 原版对 Postgres Professional 的介绍，包括其对 PostgreSQL 的贡献、产品和服务。
- [出版信息与致谢](/publication/): 原英文书的作者、英文版译者、封面设计者、版次、版权和 ISBN。
