tags:

views:

45

answers:

2

Is it possible to change the postgresql role a user is using when interacting with postgres after the initial connection?

The database(s) will be used in a web application and I'd like to employ database level rules on tables and schemas with connection pooling. From reading the postgresql documentation it appears I can switch roles if I originally connect as a user with the superuser role, but I would prefer to initially connect as a user with minimal permissions and switch as necessary. Having to specify the user's password when switching would be fine (in fact I'd prefer it).

What am I missing?

Update: I've tried both SET ROLE and SET SESSION AUTHORIZATION as suggested by @Milen however neither command seems to work if the user is not a superuser:

$ psql -U test
psql (8.4.4)
Type "help" for help.

test=> \du test
          List of roles
 Role name | Attributes |   Member of    
-----------+------------+----------------
 test      |            | {connect_only}

test=> \du test2
          List of roles
 Role name | Attributes |   Member of    
-----------+------------+----------------
 test2     |            | {connect_only}

test=> set role test2;
ERROR:  permission denied to set role "test2"
test=> \q
+1  A: 

Take a look at "SET ROLE" and "SET SESSION AUTHORIZATION".

Milen A. Radev
I've tried both commands to no avail, if the connecting user isn't a superuser I get permission denied errors. I've updated the question with additional information
Chris Gow
I believe your user has to be a member of the ROLES you already want to set and have the NOINHERITS property on their user so that they're not default on. Then you should be escalate and de-escalate. But they have to be a member of the ROLE already.
rfusca
A: 

I'd look at this page.

rfusca
The link above describes how to use roles in postgresql which I originally misunderstood
Chris Gow