Postgres - 在 bash 中测试数据库连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26911508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Postgres - testing database connection in bash
提问by Raphael
I wonder if there is an alternative to the psql command to test the connection to a postgresql database using bash.
我想知道是否有 psql 命令的替代方法来使用 bash 测试与 postgresql 数据库的连接。
I'm setting up a Core OS cluster and have a side service which should perform the equivalent of psql 'host=xxx port=xxx dbname=xxx user=xxx'
every minute to determine if the service is running, and more important, if one can connect to it using the given parameters).
我正在设置一个 Core OS 集群并有一个辅助服务,它应该psql 'host=xxx port=xxx dbname=xxx user=xxx'
每分钟执行一次,以确定服务是否正在运行,更重要的是,如果可以使用给定的参数连接到它)。
I cannot install postgres directly on Core OS. The command usually used in Core OS is something like curl -f ${COREOS_PUBLIC_IPV4}:%i;
. But it tells only if the service itself is running on the given port, without any access check.
我无法直接在 Core OS 上安装 postgres。Core OS 中通常使用的命令类似于curl -f ${COREOS_PUBLIC_IPV4}:%i;
. 但它只告诉服务本身是否在给定的端口上运行,而不进行任何访问检查。
Thank you in advance!
先感谢您!
回答by alibaba
pg_isready
is a utility for checking the connection status of a PostgreSQL database server. The exit status specifies the result of the connection check.
pg_isready
是一个用于检查 PostgreSQL 数据库服务器连接状态的实用程序。退出状态指定连接检查的结果。
It can easily be used in bash. PostgresSQL Docs - pg_isready
它可以很容易地在 bash 中使用。PostgresSQL 文档 - pg_isready
Example Usage:
示例用法:
pg_isready -d <db_name> -h <host_name> -p <port_number> -U <db_user>
Exit Status
退出状态
pg_isready
returns the following to the shell:
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).
回答by David Chan
you can write a simple connection script in your language of choice.
您可以使用您选择的语言编写一个简单的连接脚本。
hopefully your Core OS system has one of perl, php, python, ruby, etc installed
希望您的 Core OS 系统安装了 perl、php、python、ruby 等之一
here is one in python:
这是python中的一个:
#!/usr/bin/python2.4
#
import psycopg2
try:
db = psycopg2.connect("dbname='...' user='...' host='...' password='...'")
except:
exit(1)
exit(0)
now your cmdline looks like this
现在你的 cmdline 看起来像这样
python psqltest.py && echo 'OK' || echo 'FAIL'
回答by J.C.
You can build a simple container that extends the first (to conserve disk) to perform the check. For example:
您可以构建一个简单的容器来扩展第一个(以节省磁盘)来执行检查。例如:
FROM postgres
ENTRYPOINT [ "psql", "-h", "$POSTGRES_PORT_5432_TCP_ADDR", "-p", "$POSTGRES_PORT_5432_TCP_PORT" ]
If you're using a different image than postgres
, of course use that one. You can use pretty much any command line you like and still check exit codes from bash on the CoreOS host:
如果您使用的图像与 不同postgres
,当然使用那个图像。您几乎可以使用任何您喜欢的命令行,并且仍然可以从 CoreOS 主机上的 bash 中检查退出代码:
#!/bin/sh
if ! docker run --link postgres:postgres psql --command "select * from foo;" ; then
# Do something
fi
回答by Gene
To see the connection information in PSQL Shell (psql) use \c conninfo
要查看 PSQL Shell (psql) 中的连接信息,请使用 \c conninfo