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.