pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Run external command as part of an sql statement ?
4+ messages / 4 participants
[nested] [flat]

* Run external command as part of an sql statement ?
@ 2018-05-07 21:35  David Gauthier <davegauthierpg@gmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: David Gauthier @ 2018-05-07 21:35 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi:

At the psql prompt, I can do something like...
   "select  \! id -nu"
...to get the uid of whoever's running psql.

I want to be able to run a shell command like this from within a stored
procedure.  Is there a way to do this ?

Thanks

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Run external command as part of an sql statement ?
@ 2018-05-07 21:41  David G. Johnston <david.g.johnston@gmail.com>
  parent: David Gauthier <davegauthierpg@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: David G. Johnston @ 2018-05-07 21:41 UTC (permalink / raw)
  To: David Gauthier <davegauthierpg@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Mon, May 7, 2018 at 2:35 PM, David Gauthier <davegauthierpg@gmail.com>
wrote:

> Hi:
>
> At the psql prompt, I can do something like...
>    "select  \! id -nu"
> ...to get the uid of whoever's running psql.
>
> I want to be able to run a shell command like this from within a stored
> procedure.  Is there a way to do this ?
>
>
​In core, you can probably use the untrusted​ version of Perl, Python, or
Tcl to accomplish your goal.  SQL and pl/pgSQL do not provide that
capability.​

​David J.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Run external command as part of an sql statement ?
@ 2018-05-07 21:52  Tom Lane <tgl@sss.pgh.pa.us>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2018-05-07 21:52 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: David Gauthier <davegauthierpg@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Mon, May 7, 2018 at 2:35 PM, David Gauthier <davegauthierpg@gmail.com>
> wrote:
>> I want to be able to run a shell command like this from within a stored
>> procedure.  Is there a way to do this ?

> In core, you can probably use the untrusted​ version of Perl, Python, or
> Tcl to accomplish your goal.  SQL and pl/pgSQL do not provide that
> capability.​

Depending on what you want to do, COPY TO/FROM PROGRAM might be a
serviceable option.  But, just like the untrusted-PL variants, you
need to be superuser.  Keep in mind that the program will run as
the database server owner (which is the reason for the superuser
restriction).

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Run external command as part of an sql statement ?
@ 2018-05-07 22:37  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: David Gauthier <davegauthierpg@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Adrian Klaver @ 2018-05-07 22:37 UTC (permalink / raw)
  To: David Gauthier <davegauthierpg@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On 05/07/2018 02:35 PM, David Gauthier wrote:
> Hi:
> 
> At the psql prompt, I can do something like...
>     "select  \! id -nu"
> ...to get the uid of whoever's running psql.
> I want to be able to run a shell command like this from within a stored 
> procedure.  Is there a way to do this ?

PL/sh?:

https://github.com/petere/plsh

CREATE OR REPLACE FUNCTION id() RETURNS text AS '
#!/bin/sh
id -nu
' LANGUAGE plsh;

aklaver@tito:~> psql -d test -U postgres

test=# select * from id();
     id
----------
  postgres


It is an untrusted language so you will need to run as a superuser.


> 
> Thanks


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2018-05-07 22:37 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-05-07 21:35 Run external command as part of an sql statement ? David Gauthier <davegauthierpg@gmail.com>
2018-05-07 21:41 ` David G. Johnston <david.g.johnston@gmail.com>
2018-05-07 21:52   ` Tom Lane <tgl@sss.pgh.pa.us>
2018-05-07 22:37 ` Adrian Klaver <adrian.klaver@aklaver.com>

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