pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Achilleas Mantzios <achill@matrix.gatewaynet.com>
To: pgsql-admin@postgresql.org
Subject: Re: Querying the hostname of the server
Date: Fri, 30 Apr 2010 17:01:26 +0200
Message-ID: <201004301801.26710.achill@matrix.gatewaynet.com> (raw)
In-Reply-To: <292551.66852.qm@web23604.mail.ird.yahoo.com>
References: <292551.66852.qm@web23604.mail.ird.yahoo.com>

Yes, nice and simple.

I just did this in C and it works ok!

hostname.c
===========================================

#include <unistd.h>
#include "postgres.h"
#include "utils/elog.h"
#include "utils/palloc.h"
#include "storage/bufpage.h"
#define MAX_HOST_SIZE   200

PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(hostname);
Datum hostname(PG_FUNCTION_ARGS);

Datum
hostname(PG_FUNCTION_ARGS)
{
        int len;
        char buf[MAX_HOST_SIZE + 1];
        text *result;

        gethostname(buf,MAX_HOST_SIZE);
        len = strlen(buf);
        //elog(NOTICE, "hostname=%s\n",buf);
        result=(text *)palloc(len + VARHDRSZ);
        SET_VARSIZE(result, len + VARHDRSZ);
        memcpy(VARDATA(result),buf,strlen(buf));
        PG_RETURN_POINTER(result);
}


Makefile
===========================================
MODULE_big = hostname
OBJS = hostname.o

ifdef USE_PGXS
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
else
subdir = /var/lib/pgsql/src/Ccode/hostname
top_builddir = /usr/local/src/postgresql-8.3.3
include $(top_builddir)/src/Makefile.global
include $(top_srcdir)/contrib/contrib-global.mk
endif


hostname.sql
==================================================
CREATE OR REPLACE FUNCTION hostname() RETURNS text
    AS '$libdir/hostname', 'hostname'
    LANGUAGE c IMMUTABLE STRICT;


Στις Friday 30 April 2010 17:43:49 ο/η Glyn Astill έγραψε:
> --- On Fri, 30/4/10, Ross J. Reedstrom <reedstrm@rice.edu> wrote:
> 
> > > Hi,
> > > 
> > > I have a number of PostgreSQL servers which I often
> > access through ssh
> > > tunnel with Pgadmin3. I would like to double check
> > which one I have landed
> > > on (if the tunnel is really configured the way I
> > want). Is there a way to
> > > query the hostname from the catalogs?
> > 
> > Hmm, that's a bit tricky, since I assume you're using a
> > local db
> > connection inside the tunnel, so inet_server_addr()
> > probably returns
> > null. If you're talking unix/linux machines, then
> > /etc/hostname _should_
> > have the current hostname in it, so:
> > 
> > create temp table foo (t text);
> > copy foo from '/etc/hostname';
> > select * from foo;
> > drop table foo;
> > 
> > Should work.
> > 
> 
> Or you could do something like:
> 
> CREATE OR REPLACE FUNCTION hostname()
> RETURNS text AS
> $BODY$
> 	$host = `hostname`;
>         return $host;
> $BODY$
> LANGUAGE 'plperlu';
> 
> 
> 
> 
> 
> 



-- 
Achilleas Mantzios



view thread (7+ messages)  latest in thread

Message-ID: <201004301801.26710.achill@matrix.gatewaynet.com>
Permalink:  ../201004301801.26710.achill@matrix.gatewaynet.com/
Also on:    postgresql.org/message-id/201004301801.26710.achill@matrix.gatewaynet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: achill@matrix.gatewaynet.com
  Subject: Re: Querying the hostname of the server
  In-Reply-To: <201004301801.26710.achill@matrix.gatewaynet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox