20

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

1

4 Answers 4

11

You need to execute following query to get the hostname:

SELECT boot_val,reset_val
FROM pg_settings
WHERE name='listen_addresses';;
3
  • 1
    I have corrected my answer to get the hostname info directly from postgresql. However it will only display what is configured in postgresql.conf. In order to get accurate hostname of server one must check hostname command on shell. Commented Sep 12, 2018 at 13:05
  • 13
    Well, in my case that simply returns localhost ;)
    – user1822
    Commented Sep 12, 2018 at 13:15
  • This worked on my Windows 10 machine. Note I also used this link for help in order to connect pgadmin to the DB on my personal machine: docs.aws.amazon.com/AmazonRDS/latest/UserGuide/…
    – nate
    Commented Jul 19, 2019 at 18:20
5

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().

5

I use such a solution. No plugins required. Temporary tables are not needed. Only for unix.

select pg_read_file('/etc/hostname') as hostname;
3
  • 3
    This will only work for the database superuser.
    – user1822
    Commented Nov 7, 2022 at 12:40
  • 2
    Since this only works for the superuser, if you're on AWS RDS, you only have an admin account which is not the superuser, so this does not work at all / on rds.
    – scravy
    Commented May 13, 2023 at 7:27
  • @user1822: Not just only for superusers, but also not on windows.
    – Quandary
    Commented Aug 25, 2023 at 21:31
1

I've solve this issue after creating a temp table and filling it with the result of copy from

-- creating a temporary table and only one column
# CREATE TEMP TABLE tt_cmd (hostname text);
CREATE TABLE
-- filling the column with the result of the command
# COPY tt_cmd FROM PROGRAM 'hostname';
COPY 1
-- checking the result
# SELECT * from tt_cmd ;
+-------------+
|  hostname   |
+-------------+
| db_hostname |
+-------------+
-- filling the result into a variable
# SELECT * FROM tt_cmd \gset
-- no return
-- checking the result
# \echo :hostname
db_hostname
-- dropping the temporary table
DROP TABLE tt_cmd ;
DROP TABLE
-- the variable is still here with the right result
# \echo :hostname
db_hostname

And I can use the result like this, to check, for example, if I'm running on a production server.

SELECT CASE :'hostname' WHEN 'prod_db_hostname' THEN 'PROD/' ELSE '' END AS sub_dir
\gset

After reading the comments, the answer is right if you're executing on Pg server. That's the case for me. Of course, the hostname result is where you're executing it. Maybe there's a way with HOST variable value using it in psql.

SELECT CASE WHEN :'HOST' ~* '^server_prod_hostname' THEN '/on_prod' ELSE '/offprod' END AS dir;
3
  • 2
    \! hostname returns hostname where the psql is running. Commented Jul 30, 2022 at 9:59
  • Your answer is a bit confusing, first you give the wrong advice \! hostname - return client hostname, and then you give the right answer. This command does what is needed - COPY tt_cmd (ret) FROM PROGRAM 'hostname';
    – Arty
    Commented Aug 5, 2022 at 8:07
  • 1
    Sorry, I've check on a server, not a client. So, happy to know that the command is the right anwser.
    – Pct Mtnxt
    Commented Aug 8, 2022 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.