Monday, December 26, 2011

Postgresql - System Information Functions - querying for an IP

I need to ask the PostgreSQL server for its IP. One answer I found is using one of the "System Information Functions" called inet_server_addr().

The big gotcha regarding this function is that it returns NULL should the connection be made via a unix-domain socket.

inet_client_addr returns the IP address of the current client, and inet_client_port returns the port number. inet_server_addr returns the IP address on which the server accepted the current connection, and inet_server_port returns the port number. All these functions return NULL if the current connection is via a Unix-domain socket.

As a side note, the array of functions to get information about the system is pretty cool.

http://www.postgresql.org/docs/9.1/static/functions-info.html

Among many other things you can issue queries about the session, access privileges, comments, schema visibility, the catalog, transaction id's and snapshots.

I guess I'll need to do it the linux way or figure out how to use something else than unix-domain sockets for my connections.

This sort of does the trick:

/sbin/ifconfig eth0 | grep inet | awk '{print $2}' | sed 's/addr://'

But it relies on two assumptions: that there's an eth0 interface and that the node is connected through that interface to the cluster's network.

http://www.cyberciti.biz/faq/bash-shell-command-to-find-get-ip-address/

No comments:

Post a Comment