Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create duplicate role of a user in postgres

I need a new user but it should be granted all those privileges that the other existing user/role has.

e.g.

  • User A has SELECT privileges on Table1
  • User A has EXECUTE privileges on Table2
  • ...

If a new User B is created, I need the same privileges as,

  • User B has SELECT privileges on Table1
  • User B has EXECUTE privileges on Table2
  • ...

Dont ask why :/

Actually User A has custom privileges on different tables, schemas, and functions; so its very tedious and lengthy process to manually grant permissions to the new user. Any help would be good.

like image 541
UserBSS1 Avatar asked May 16 '13 15:05

UserBSS1


2 Answers

Try something like:

GRANT A TO B;

It will grant all right of role A to B.

For details read this chapter of the manual.

like image 145
Ihor Romanchenko Avatar answered Sep 27 '22 18:09

Ihor Romanchenko


First understand that roles and users are the same thing. In fact there isn't a thing called a user really, it's just a ROLE with a LOGIN option.

Second roles can be granted to other roles.

Third priviledges on roles can be inherited.

So assuming you have created your user a like:

CREATE ROLE A LOGIN;
GRANT SELECT ON table1 TO a;
GRANT EXECUTE ON FUNCTION xxx TO a;

You should be able to create a second role that mirrors the first role like:

CREATE ROLE b LOGIN;
GRANT a TO b;
like image 42
Chris Farmiloe Avatar answered Sep 27 '22 16:09

Chris Farmiloe