> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/reference/reference-server/app-pgchecksums.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# pg\_checksums

### pg\_checksums

pg\_checksums — 在 PostgreSQL 資料庫叢集中啟用、停用或檢查資料校驗和

### 語法

`pg_checksums` \[*`option`*...] \[\[ `-D` | `--pgdata` ]*`datadir`*]

### 說明

pg\_checksums 會檢查、啟用或停用 PostgreSQL 叢集中的資料校驗和。執行 pg\_checksums 之前，伺服器必須已正常（clean）關閉。驗證校驗和時，若沒有校驗和錯誤，結束狀態為零；若偵測到至少一個校驗和失敗，結束狀態則為非零。啟用或停用校驗和時，若操作失敗，結束狀態為非零。

驗證校驗和時，會掃描叢集中的每一個檔案。啟用校驗和時，每個校驗和有變更的關聯檔案區塊都會被就地改寫。停用校驗和只會更新檔案 `pg_control`。

### 選項

可用的命令列選項如下：

`-D directory`\
`--pgdata=directory` : 指定資料庫叢集的儲存目錄。

`-c`\
`--check` : 檢查校驗和。若未指定其他模式，這是預設模式。

`-d`\
`--disable` : 停用校驗和。

`-e`\
`--enable` : 啟用校驗和。

`-f filenode`\
`--filenode=filenode` : 只驗證 filenode 為 *`filenode`* 之關聯中的校驗和。

`-N`\
`--no-sync` : 預設情況下，`pg_checksums` 會等待所有檔案都安全地寫入磁碟。此選項會讓 `pg_checksums` 不等待就返回，這樣比較快，但也表示之後若作業系統當機，可能會使更新過的資料目錄損毀。一般而言，此選項適用於測試，但不應該用於正式環境的安裝。使用 `--check` 時，此選項沒有作用。

`-P`\
`--progress` : 啟用進度回報。開啟此選項後，會在檢查或啟用校驗和時提供進度報告。

`--sync-method=method`

設定為預設值 `fsync` 時，`pg_checksums` 會遞迴地開啟並同步資料目錄中的所有檔案。搜尋檔案時，會跟隨 WAL 目錄與每個已設定資料表空間的符號連結。

在 Linux 上，可以改用 `syncfs`，要求作業系統同步包含資料目錄、WAL 檔案與每個資料表空間的整個檔案系統。使用 `syncfs` 時需要注意的事項，請參閱 [recovery\_init\_sync\_method](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-error-handling.md#GUC-RECOVERY-INIT-SYNC-METHOD)。

使用 `--no-sync` 時，此選項沒有作用。

`-v`\
`--verbose` : 啟用詳細輸出。列出所有已檢查的檔案。

`-V`\
`--version` : 印出 pg\_checksums 的版本後結束。

`-?`\
`--help` : 顯示 pg\_checksums 命令列引數的說明後結束。

### 環境

`PGDATA` : 指定資料庫叢集的儲存目錄；可以用 `-D` 選項覆寫。

`PG_COLOR` : 指定診斷訊息是否使用色彩。可用值為 `always`、`auto` 與 `never`。

### 注意事項

在大型叢集中啟用校驗和可能會花費很長的時間。在此操作期間，不得啟動該叢集或其他會寫入資料目錄的程式，否則可能會發生資料遺失。

在使用會直接複製關聯檔案區塊之工具（例如 [pg\_rewind](https://docs.postgresql.tw/19/reference/reference-server/app-pgrewind.md)）的複寫架構中，若啟用或停用校驗和的操作未在所有節點上一致地進行，可能會導致以錯誤校驗和形式呈現的頁面損毀。因此，在複寫架構中啟用或停用校驗和時，建議先停止所有叢集，再一致地切換所有叢集。銷毀所有待命伺服器、在主要伺服器上執行該操作，最後從頭重新建立待命伺服器，這種做法也是安全的。

若 pg\_checksums 在啟用或停用校驗和時被中止或終止，叢集的資料校驗和設定會維持不變，而且可以重新執行 pg\_checksums 來進行相同的操作。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/app-pgchecksums.html)（原文版本：18.6；核對日期：2026-10-05）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/reference/reference-server/app-pgchecksums.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
