database 检查 Postgresql 是否正在监听
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17303531/
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
Check if Postgresql is listening
提问by ksun
Given an IP Address and port number, is it possible to check if the machine with that IP address has Postgresql listening on the specified port? If so, how?
给定 IP 地址和端口号,是否可以检查具有该 IP 地址的机器是否有 Postgresql 侦听指定端口?如果是这样,如何?
I just want to obtain a boolean value of whether Postgresql is listening on the specified port of the specified machine.
我只想获取一个布尔值,表示 Postgresql 是否正在侦听指定机器的指定端口。
回答by
You can use, for example, nmaptool:
例如,您可以使用nmap工具:
=$ sudo nmap -v -p 5930 127.0.0.1
Starting Nmap 6.00 ( http://nmap.org ) at 2013-06-25 19:28 CEST
Initiating SYN Stealth Scan at 19:28
Scanning localhost (127.0.0.1) [1 port]
Discovered open port 5930/tcp on 127.0.0.1
Completed SYN Stealth Scan at 19:28, 0.03s elapsed (1 total ports)
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000045s latency).
PORT STATE SERVICE
5930/tcp open unknown
Read data files from: /usr/bin/../share/nmap
Nmap done: 1 IP address (1 host up) scanned in 0.08 seconds
Raw packets sent: 1 (44B) | Rcvd: 2 (88B)
Alternatively you can just "SELECT 1" with psql, and check output:
或者,您可以使用 psql 仅“选择 1”,然后检查输出:
=$ psql -h 127.0.0.1 -p 5930 -c "select 1"
?column?
----------
1
(1 row)
=$ psql -h 127.0.0.1 -p 5940 -c "select 1"
psql: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5940?
回答by Craig Ringer
I think you need to define what you're trying to achieve better. Do you just want to know if anythingis listening on a certain point? If PostgreSQLis listening on a given port? If PostgreSQL is running and actually accepting connections? If you can connect to PostgreSQL, authenticate successfully and issue queries?
我认为你需要定义你想要实现的更好的目标。你只是想知道在某个点上是否有什么东西在听吗?如果PostgreSQL正在侦听给定的端口?如果 PostgreSQL 正在运行并实际接受连接?如果您可以连接到 PostgreSQL,验证成功并发出查询?
One option is to invoke psqlto connect to it and check the result code. Do not attempt to parse the output text, since that's subject to translation into different languages.
一种选择是调用psql以连接到它并检查结果代码。不要尝试解析输出文本,因为它会被翻译成不同的语言。
Better, use the client library for the language of your choice - psycopg2for Python, PgJDBC for Java, the Pg gem for Ruby, DBD::Pgfor Perl, nPgSQL for C#, etc. This is the approach I'd recommend. The SQLSTATE or exception details from any connection error will tell you more about why the connection failed - you'll be able to tell the difference between the server not listening, authentication failure, etc this way. For example, in Python:
更好的是,使用您选择的语言的客户端库 -psycopg2用于 Python、用于 Java 的 PgJDBC、用于 Ruby 的 Pg gem、DBD::Pg用于 Perl、用于 C# 的 nPgSQL 等。这是我推荐的方法。来自任何连接错误的 SQLSTATE 或异常详细信息将告诉您有关连接失败原因的更多信息 - 您将能够通过这种方式区分服务器未侦听、身份验证失败等。例如,在 Python 中:
import psycopg2
try:
conn = psycopg2.connect("host=localhost dbname=postgres")
conn.close();
except psycopg2.OperationalError as ex:
print("Connection failed: {0}".format(ex))
There are exception details in ex.pgcode(the SQLSTATE) to tell you more about errors that're generated server-side, like authentication failures; it'll be empty for client-side errors.
ex.pgcode(the SQLSTATE) 中有异常详细信息,可以告诉您有关服务器端生成的错误的更多信息,例如身份验证失败;对于客户端错误,它将为空。
If you just want to see if somethingis listening on a given IP and TCP port, you can use netcat(*nix only), or a simple script in the language of your choice that creates a socket and does a connect() then closes the socket if it gets a successful response. For example, the following trivial Python script:
如果你只是想看看什么是给定的IP地址和TCP端口上侦听,您可以使用netcat(*仅限nix中),或在你选择的是创建一个套接字,并做了连接(语言一个简单的脚本),然后关闭如果它得到成功的响应。例如,以下简单的 Python 脚本:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(('localhost',5432))
s.close()
except socket.error as ex:
print("Connection failed with errno {0}: {1}".format(ex.errno, ex.strerror))
The same approach applies in any programming language, just the details of the socket library and error handling vary.
同样的方法适用于任何编程语言,只是套接字库和错误处理的细节有所不同。
For some purposes it can also be useful to use the netstattool to passively list which processes are listening on which network sockets. The built-in netstaton Windows is pretty brain-dead so you have to do more parsing of the output than with netstatfor other platforms, but it'll still do the job. The presence of a socket in netstatdoesn't mean that connecting to it will succeed, though; if the process has failed in some way that leaves it broken but still running (stuck in an infinite loop, blocked by a debugger, SIGSTOPed, etc) then it won't respond to an actual connection attempt.
出于某些目的,使用该netstat工具被动列出哪些进程正在侦听哪些网络套接字也很有用。netstatWindows 上的内置程序非常脑残,因此与netstat其他平台相比,您必须对输出进行更多解析,但它仍然可以完成这项工作。但是,套接字的存在netstat并不意味着连接到它会成功。如果进程以某种方式失败,使其中断但仍在运行(陷入无限循环,被调试器、SIGSTOPed 等阻止),那么它不会响应实际的连接尝试。
回答by Nam G VU
In brief
简单来说
In details
详情
Fastest way is to use netcataka ncwith timeout ability as shared here
最快的方法是使用netcatakanc与这里共享的超时功能
Results as 0/1 means postgres working/not-working
结果为 0/1 表示 postgres 工作/不工作
echo 'QUIT' | nc -w SECONDS YOUR_HOST PORT; echo $?
# eg
echo 'QUIT' | nc -w 1 localhost 5432; echo $?
Another also-faster way that works for me is to use telnetas discussed here.
另一种对我有用的更快的方法是使用这里telnet讨论的。
echo -e '\x1dclose\x0d' | telnet YOUR_HOST PORT
# eg
echo -e '\x1dclose\x0d' | telnet localhost 5432

