I need to check if a specific user has permissions to create a database in Postgres. How can i accomplish that?
2 Answers
Try this:
select rolcreatedb
from pg_authid
where rolname = 'your user name'
5 Comments
Asclepius
For the current user, use
rolname = current_user.Daniel Vérité
Use
pg_roles instead of pg_authid if you're not superuser.mfcallahan
What I use to quickly check the permissions on the current role:
SELECT * FROM pg_roles WHERE rolname = current_userjian
yech. pg_roles is public for all. @mfcallahan
user5480949
Worth noting that
rolcreatedb can be false for super users but they can still create a database. Selecting rolcreatedb OR rolsuper should work betterThere are database functions that can do this for you eg:
has_database_privilege(user, database, privilege)
See here: http://www.postgresql.org/docs/8.3/static/functions-info.html for a list of functions and here: http://www.postgresql.org/docs/8.3/static/ddl-priv.html for the privileges to test against.
hth
2 Comments
Faris Zacina
I don't have a database parameter to provide to that function. I want to check if there is a permission on server level, not db level.
mcalex
This may be the function you are after, then:
pg_has_role(user, role, privilege) (from the same page).