HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to get hostname in postgresql?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlgethostnamehow

Problem

Is there any way where I can get hostname/servername in postgresql like @@ServerName in SQL Server?

Solution

PostgreSQL doesn't provide a builtin function returning the server's hostname.

A pg_gethostname() function written in C (a wrapper to POSIX's gethostname()) has been contributed on the wiki and made available as an extension on PGXN:

https://wiki.postgresql.org/wiki/Pg_gethostname

https://pgxn.org/dist/hostname/

If you can't install that on the server, or an equivalent in another language supporting calls to the system such as plperlu or plpythonu, you're probably going to be limited to inet_server_addr().

Context

StackExchange Database Administrators Q#217255, answer score: 5

Revisions (0)

No revisions yet.