pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Charles Clavadetscher <clavadetscher@swisspug.org>
To: 'Durumdara' <durumdara@gmail.com>
To: pgsql-general@postgresql.org
Subject: RE: Set role dynamically from proc
Date: Wed, 22 Nov 2017 15:09:11 +0100
Message-ID: <026b01d3639b$84c21930$8e464b90$@swisspug.org> (raw)
In-Reply-To: <CAEcMXhkON0g+0a8y05ZyhAi9HxQrxKU+gp8zjxC9+S0dPpALmA@mail.gmail.com>
References: <CAEcMXhkON0g+0a8y05ZyhAi9HxQrxKU+gp8zjxC9+S0dPpALmA@mail.gmail.com>

Hello

 

From: Durumdara [mailto:durumdara@gmail.com] 
Sent: Mittwoch, 22. November 2017 14:56
To: pgsql-general@postgresql.org
Subject: Set role dynamically from proc

 

Hello!

 

May you know the way how to set role dynamically.

 

DO 

$$

DECLARE act_dbowner varchar(100);

BEGIN

 

            SELECT u.usename into act_dbowner FROM pg_database d

            JOIN pg_user u ON (d.datdba = u.usesysid)

            WHERE d.datname = (SELECT current_database());

            raise notice 'DB owner: %', act_dbowner;

 

            set role to act_dbowner; --  THIS LINE

            

END

$$;

 

-------------

 

ERROR:  role "act_dbowner" does not exist

CONTEXT:  SQL statement "set role to act_dbowner"

PL/pgSQL function inline_code_block line 10 at SQL statement

 

I try to use $act_dbowner, but it have no effect.

 

It seems that the user does not exist:

 

CREATE OR REPLACE FUNCTION set_role()

RETURNS VOID

AS $$

BEGIN

  RAISE NOTICE 'CURRENT_USER: %', (select current_user);

  SET ROLE blabla;

  RAISE NOTICE 'CURRENT_USER: %', (select current_user);

END;

$$ LANGUAGE plpgsql;

 

db=> select * from set_role();

NOTICE:  CURRENT_USER: kofadmin

ERROR:  role "blabla" does not exist

CONTEXT:  SQL statement "SET ROLE blabla"

PL/pgSQL function set_role() line 4 at SQL statement

 

db=> CREATE ROLE blabla;

CREATE ROLE

 

db=> select * from set_role();

NOTICE:  CURRENT_USER: kofadmin

ERROR:  permission denied to set role "blabla"

CONTEXT:  SQL statement "SET ROLE blabla"

PL/pgSQL function set_role() line 4 at SQL statement

 

db=> GRANT blabla TO kofadmin;

GRANT ROLE

 

Now it works:

 

db=> select * from set_role();

NOTICE:  CURRENT_USER: kofadmin

NOTICE:  CURRENT_USER: blabla

[...]

 

Regards

Charles

 

Thank you for your help!

 

Best wishes

   dd

view thread (5+ messages)  latest in thread

Message-ID: <026b01d3639b$84c21930$8e464b90$@swisspug.org>
Permalink:  ../026b01d3639b$84c21930$8e464b90$@swisspug.org/
Also on:    postgresql.org/message-id/026b01d3639b$84c21930$8e464b90$@swisspug.org

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: clavadetscher@swisspug.org, durumdara@gmail.com
  Subject: Re: Set role dynamically from proc
  In-Reply-To: <026b01d3639b$84c21930$8e464b90$@swisspug.org>
  References: <CAEcMXhkON0g+0a8y05ZyhAi9HxQrxKU+gp8zjxC9+S0dPpALmA@mail.gmail.com> <026b01d3639b$84c21930$8e464b90$@swisspug.org>

* 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