3

I need to check if a specific user has permissions to create a database in Postgres. How can i accomplish that?

2 Answers 2

7

Try this:

select rolcreatedb
from pg_authid 
where rolname = 'your user name'
Sign up to request clarification or add additional context in comments.

5 Comments

For the current user, use rolname = current_user.
Use pg_roles instead of pg_authid if you're not superuser.
What I use to quickly check the permissions on the current role: SELECT * FROM pg_roles WHERE rolname = current_user
yech. pg_roles is public for all. @mfcallahan
Worth noting that rolcreatedb can be false for super users but they can still create a database. Selecting rolcreatedb OR rolsuper should work better
1

There 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

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.
This may be the function you are after, then: pg_has_role(user, role, privilege) (from the same page).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.