views:

75

answers:

3

Is it possible with Postgresql to create a database which has 2 users which act like owners to the database?

I can create a group role and add both users to that group, and then make the group the owner of the database, but this requires both users to be have to manually set their role on every connection to make any tables they have created accessible to the other user. Is there any way to make the group be the default role for a user each time they log in or any other way to achieve the same thing?

A: 

Do the users really need to be "owners", or can they just both be super-users on that database?

Tim Drisdelle
If them both being superusers on the database would achieve the same thing, then yes that would work :)How would I achieve that?
David Reynolds
Well, the superuser is across the whole server, not just on the single database. Not sure if this is still a good solution for you...
Tim Drisdelle
No, that wouldn't work then.
David Reynolds
+1  A: 

Ah, found it: PostgreSQL Docs: Chapter 20. Database Roles and Privileges

"member roles that have the INHERIT attribute automatically have use of privileges of roles they are members of."

CREATE ROLE joe LOGIN INHERIT;
CREATE ROLE admin NOINHERIT;
GRANT admin TO joe;

"Immediately after connecting as role joe, a database session will have use of privileges granted directly to joe plus any privileges granted to admin, because joe "inherits" admin's privileges."

Tim Drisdelle
True, but tables created by joe are owned by joe and not admin, which means any other users in the group don't have access to them.
David Reynolds
But you can have 'admin' with rights to do everything on all tables, which would include tables created by other users in the group.
Tim Drisdelle
But you still have to manually set the admin role for each connection you make with the user don't you?
David Reynolds
No, that's what the INHERIT means. Go read that documentation page on Roles and Privileges - it's clear."The members of a role can use the privileges of the group role in two ways. First, every member of a group can explicitly do SET ROLE to temporarily "become" the group role."..."Second, member roles that have the INHERIT attribute automatically have use of privileges of roles they are members of. "
Tim Drisdelle
Yes, they have the privileges of the group, but they are still the sole owner of any tables they create. I have investigated this avenue, in fact I describe it in the original question. Ideally what I need to happen is that set role admin is automatically executed on each client connection.
David Reynolds
>>> member roles that have the INHERIT attribute automatically have use of privileges of roles they are members of <<< which means that they do not need to explicitly do SET ROLE.
Tim Drisdelle
+2  A: 

No, each database can only have one owner. As stated previously you can have more than one superuser, or you can grant permissions specifically to group roles that are then inherited.

You might want to look at http://blog.hagander.net/archives/70-Faking-the-dbo-role.html, for a way to fake something similar to what you're asking for. It's not perfect, but it might be good enough for you. It should be able to solve the object-ownership problem at least.

Magnus Hagander
Yes, that appears to do what I want. Any security implications of working this way that you can think of?
David Reynolds
No, that should be fine. I assume that if you make them admins, they are trusted not to intentionally break things. Like, the user can still revert his role to the original one if he has malicious intent.
Magnus Hagander