The ALTER DEFAULT PRIVILEGES command allows us to set the privileges that will be applied to objects created in the future. It’s important to note that this does not affect privileges assigned to existing objects; default privileges can be set globally for objects created in the current database or in specified schemas.

There are many misconceptions about the behavior of default privileges among database users, and I’ve often heard complaints that users don’t have access even after assigning default privileges for a specific schema. In this blog, we will clarify these doubts about default privileges in PostgreSQL.

Understanding default privileges

When an object is created, it is assigned an owner. Initially, the owner is the role that executes the create object statement (which can be changed later). For most kinds of objects, only the owner (or a superuser) has all the privileges of the object. However, this behavior can be changed by altering the default privileges with the ALTER DEFAULT PRIVILEGES statement. This allows us to assign any privileges by default to any intended user on any kind of object. Currently, only the privileges for schemas, tables (including views and foreign tables), sequences, functions, and types (including domains) can be altered with ALTER DEFAULT PRIVILEGES.

Example:

Now test the permission for role obj_user:

Need to grant permission explicitly:

In the above example, the user obj_user did not get the SELECT permission by default, and we had to grant the permission explicitly. If this is always the case, we should consider automating the process to grant the SELECT permission on tables in the test schema. This is possible with ALTER DEFAULT PRIVILEGES.

Setting default privileges

Let’s grant default privileges and test the access on new objects again:

Yahoo! The permissions worked as expected. Now, let’s consider creating an object with a different user than postgres. Let’s create a table with another user, obj_creator, and test the permissions for obj_user:

Oh no!! Didn’t work!! This is the actual behavior of the PostgreSQL default privileges that we granted. Many users assume that as we have granted the default permissions, it will always work no matter who the object creator is.

The reason it didn’t work

Here, the user obj_user did not automatically get permission on the newly created table because the default permission only works if the object creator is the same as the executor of the ALTER DEFAULT PRIVILEGES statement (by default, the current user, in this case, postgres).

Resolving the myth

To resolve this, let’s execute the below statement, considering obj_creator will be the creator of the objects in the test schema:

Now test the access for obj_user:

It worked!! By specifying the object creator in the ALTER DEFAULT PRIVILEGES statement, we ensure that obj_user gets the SELECT privilege automatically on any new tables created by obj_creator in the test schema.

More sample statements

Similarly, just like GRANT, we can REVOKE the privileges as well.

Sample REVOKE statements

Listing out the default privileges

In PostgreSQL, the ddp command can be used to list default privileges.

Alternatively, you can use the below query to return the output in a more readable way.

Using ALTER DEFAULT PRIVILEGES

In many scenarios, we might want to set default privileges for objects created by specific roles. FOR ROLE cause is particularly useful in environments where multiple roles create objects, and each role has its own set of users with specific access requirements. Using ALTER DEFAULT PRIVILEGES, we can automate and streamline permission management, ensuring that the correct users have access to new objects without manual intervention.

Our PostgreSQL Performance Tuning eBook condenses years of database expertise into a practical guide for optimizing your PostgreSQL databases. Inside, you’ll discover our most effective PostgreSQL performance strategies derived from real-world experience.

 

Download now: Elevate your PostgreSQL Performance

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments