我想知道是否有替代 psql 命令的方法来测试使用 bash 与 postgresql 数据库的连接。
我正在设置一个 Core OS 集群并有一个辅助服务,它应该每分钟执行一次相当于 psql 'host=xxx port=xxx dbname=xxx user=xxx'
以确定是否该服务正在运行,更重要的是,如果可以使用给定的参数连接到它)。
我无法直接在 Core OS 上安装 postgres。 Core OS 中通常使用的命令类似于 curl -f ${COREOS_PUBLIC_IPV4}:%i;
。但它只告诉服务本身是否在给定端口上运行,没有任何访问检查。
提前谢谢你!
最佳答案
pg_isready
是一个用于检查 PostgreSQL 数据库服务器连接状态的实用程序。退出状态指定连接检查的结果。
它可以很容易地在 bash 中使用。 PostgresSQL Docs - pg_isready
示例用法:
pg_isready -d <db_name> -h <host_name> -p <port_number> -U <db_user>
安装
sudo apt install -y postgresql-client
退出状态
pg_isready
将以下内容返回给 shell:
0 - if the server is accepting connections normally,
1 - if the server is rejecting connections (for example during startup),
2 - if there was no response to the connection attempt, and
3 - if no attempt was made (for example due to invalid parameters).
注意:man pg_isready
声明:无需提供正确的用户名、密码或数据库名称值即可获取服务器状态;但是,如果提供的值不正确,服务器将记录失败的连接尝试。
关于bash - Postgres - 在 bash 中测试数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26911508/