Check if Postgresql is listening

Given the IP address and port number, is it possible to check whether a device with this IP address has a Postgresql listening on the specified port? If so, how?

I just want to get the boolean value of whether Postgresql is listening on the specified port of the specified machine.

+4
source share
2 answers

I think you need to determine what you want to achieve better. Do you just want to know if something is listening at a certain point? Should PostgreSQL listen on this port? Should PostgreSQL work and actually accept connections? If you can connect to PostgreSQL, authenticate successfully and issue queries?

One option is to call psql to connect to it and check the result code. Do not try to analyze the output text, as it is subject to translation into different languages.

It is better to use the client library for your language - psycopg2 for Python, PgJDBC for Java, the Pg gem for Ruby, DBD::Pg for Perl, nPgSQL for C #, etc. I would recommend this approach. SQLSTATE details or exceptions from any connection error will tell you more about the cause of the connection failure - this way you can determine the difference between the fact that the server is not listening, it does not pass authentication, etc. For example, in Python:

 import psycopg2 try: conn = psycopg2.connect("host=localhost dbname=postgres") conn.close(); except psycopg2.OperationalError as ex: print("Connection failed: {0}".format(ex)) 

ex.pgcode ( SQLSTATE ) contains details of exceptions to tell you more about errors that are generated on the server side, such as authentication errors; it will be empty for client-side errors.

If you just want to know something is listening on a given IP and TCP port, you can use netcat (* nix only) or a simple script in your language that creates a socket and executes connect () and then closes the socket if it receives successful response. For example, the following trivial Python script:

 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 is applied in any programming language, only the details of the socket library and error handling can be different.

For some purposes, it can also be useful to use the netstat tool to passively list the processes that listen on which network sockets. The built-in netstat on Windows is pretty mind-blowing, so you'll have to parse the output more than netstat for other platforms, but it will still do the job. However, having a socket in netstat does not mean that the connection to it will be successful; if the process is somehow SIGSTOP , due to which it does not work, but still works (stuck in an infinite loop, blocked by the debugger, SIGSTOP editor, etc.), it will not respond to the actual connection attempt.

+6
source

You can use, for example, the nmap tool:

 =$ 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 simply "SELECT 1" with psql and check the output:

 =$ 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? 
+8
source

Source: https://habr.com/ru/post/1488131/


All Articles