Re: Executing Shell Command - Mailing list pgsql-general

From Joe Conway
Subject Re: Executing Shell Command
Date
Msg-id 3FC806FE.5040800@joeconway.com
Whole thread Raw
In response to Executing Shell Command  ("Nicholas Walker" <nick@walkerdatanet.com>)
List pgsql-general
Nicholas Walker wrote:
> I have been trying to execute a shell command from within postgresql

You ought to be using Version 1 calling conventions -- see:
http://www.postgresql.org/docs/current/static/xfunc-c.html#AEN29226

The following works fine for me on RH9:

#define GET_STR(textp) \
   DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
extern Datum shell_exec(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(shell_exec);
Datum
shell_exec(PG_FUNCTION_ARGS)
{
     char   *cmd = GET_STR(PG_GETARG_TEXT_P(0));
     int32   result;
     result = system(cmd);
     PG_RETURN_INT32(result);
}


CREATE OR REPLACE FUNCTION xp_shellexec(text)
RETURNS int
AS '$libdir/shell_exec','shell_exec'
LANGUAGE 'C' VOLATILE STRICT;

SELECT xp_shellexec('mkdir /tmp/testing123');

[root@dev tmp]# ls -ld /tmp/test*
drwx------    2 postgres postgres     4096 Nov 28 19:31 /tmp/testing123


HTH,

Joe


pgsql-general by date:

Previous
From: "Chris Travers"
Date:
Subject: Re: PostgreSQL, MySQL, etc., was Re: PostgreSQL is much faster than MySQL, only when...
Next
From: Richard Welty
Date:
Subject: Re: [ADMIN] Misplaced modifier in Postgresql license