> 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-client/app-pg-isready.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# pg\_isready

### pg\_isready

pg\_isready — 檢查 PostgreSQL 伺服器的連線狀態

### 語法

`pg_isready` \[*`connection-option`*...] \[*`option`*...]

### 說明

pg\_isready 是用來檢查 PostgreSQL 資料庫伺服器連線狀態的工具程式。其結束狀態代表連線檢查的結果。

### 選項

`-d dbname`\
`--dbname=dbname` : 指定要連線的資料庫名稱。*`dbname`* 可以是[連線字串](https://docs.postgresql.tw/19/client-interfaces/libpq/libpq-connect/libpq-connstring.md#libpq-connstring)。若是如此，連線字串參數會覆寫任何衝突的命令列選項。

`-h hostname`\
`--host=hostname` : 指定伺服器所在機器的主機名稱。若值以斜線開頭，則會被當作 Unix 網域通訊端的目錄。

`-p port`\
`--port=port` : 指定伺服器用來接聽連線的 TCP 連接埠，或本機 Unix 網域通訊端檔案的副檔名。預設為環境變數 `PGPORT` 的值；若該變數未設定，則為編譯時指定的連接埠，通常是 5432。

`-q`\
`--quiet` : 不顯示狀態訊息。這在撰寫腳本時很有用。

`-t seconds`\
`--timeout=seconds` : 嘗試連線時，在回報伺服器沒有回應之前所等待的最大秒數。設為 0 會停用此逾時。預設為 3 秒。

`-U username`\
`--username=username` : 以使用者 *`username`* 而非預設使用者的身分連線到資料庫。

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

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

### 結束狀態

若伺服器正常接受連線，pg\_isready 會向 shell 傳回 `0`；若伺服器拒絕連線（例如在啟動期間），傳回 `1`；若連線嘗試沒有回應，傳回 `2`；若未進行任何嘗試（例如因為參數無效），則傳回 `3`。

### 環境

`pg_isready` 與大多數其他 PostgreSQL 工具程式一樣，也會使用 libpq 支援的環境變數（請參閱[第 32.15 節](https://docs.postgresql.tw/19/client-interfaces/libpq/libpq-envars.md)）。

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

### 注意事項

要取得伺服器狀態，不需要提供正確的使用者名稱、密碼或資料庫名稱值；不過，若提供的值不正確，伺服器會在日誌中記錄一次失敗的連線嘗試。

### 範例

標準用法：

```

$ pg_isready
/tmp:5432 - accepting connections
$ echo $?
0
```

使用連線參數連線到正在啟動中的 PostgreSQL 叢集：

```

$ pg_isready -h localhost -p 5433
localhost:5433 - rejecting connections
$ echo $?
1
```

使用連線參數連線到沒有回應的 PostgreSQL 叢集：

```

$ pg_isready -h someremotehost
someremotehost:5432 - no response
$ echo $?
2
```

***

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


---

# 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-client/app-pg-isready.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.
