pg_isready

一个核心实用工具,用于检查 PostgreSQL 服务器的连接状态

pg_isready 是一个 核心实用工具,用于检查 PostgreSQL 服务器的连接状态。

pg_isreadyPostgreSQL 9.3 中引入。

用法

pg_isready 使用提供的连接详细信息来建立服务器连接,并通过服务器的响应(或无响应)来确定服务器状态。

请注意,pg_isready 实际上从未连接到数据库,也不需要提供有效的数据库、用户或密码来检查服务器的响应。但是,服务器会记录连接尝试的有效性。

变更历史

pg_isready 自从在 PostgreSQL 9.3 中引入以来,没有进行实质性修改。

示例

pg_isready 的基本用法示例

postgres $ pg_isready -d 'user=postgres dbname=postgres host=localhost port=5432'
localhost:5432 - accepting connections
postgres $ echo $?
0

日志文件将包含类似以下的条目(假设 log_connections 设置为 on

2022-09-02 10:02:25.015 UTC [31325] LOG:  connection received: host=127.0.0.1 port=36628
2022-09-02 10:02:25.015 UTC [31325] LOG:  connection authorized: user=postgres database=postgres application_name=pg_isready

尝试连接到具有无效用户名和/或数据库名的服务器

postgres $ pg_isready -d 'user=nosuchuser dbname=nosuchdb host=localhost port=5432'
localhost:5432 - accepting connections
postgres $ echo $?
0

日志文件将包含类似以下的条目

2022-09-01 10:12:18.711 UTC [32711] LOG:  connection authorized: user=nosuchuser database=nosuchdb application_name=pg_isready
2022-09-01 10:12:18.711 UTC [32711] FATAL:  role "nosuchuser" does not exist

尝试连接到当前没有服务器运行的端口

postgres $ pg_isready -d 'user=postgres dbname=postgres host=localhost port=9999'
localhost:9999 - no response
postgres $ echo $?
2

分类

核心实用工具, 数据库连接, 监控

反馈

提交关于“pg_isready”的任何评论、建议或更正,请在此处 提交