bash - Postgres - 在 bash 中测试数据库连接

标签 bash postgresql docker coreos

我想知道是否有替代 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/

相关文章:

Docker 下载 Google 的 TensorFlow 问题

regex - 如何使用 grep 和正则表达式查找双引号字符串?

ruby-on-rails - 使用 ActiveRecord 和 Postgres 分组

c++ - 为什么 make 会忽略我的修改?

sql - 如何填补一系列数字之间的空白?

sql - Postgres 中真实的数学 "modulo"操作?

amazon-web-services - 在 DAG 中使用 boto3 时,Apache Airflow 无法找到 AWS 凭证

docker - 如何在Kubernetes中定义Pod环境变量并将其共享给Pod的应用

node.js - Node 冲突 : Nodejs -v works, 但不是 Node -v

bash - bash 中的变量验证(名称和 IP 地址)