Understanding Postgres Roles & Privileges

SimpleBackups founder

Laurent Lemaire

Co-founder, SimpleBackups

November 21st, 2023

PostgreSQL is a robust, open-source relational database management system widely used by organizations to store and manage data. One of PostgreSQL's key features is its powerful access control system, which allows administrators to define roles and privileges to secure and manage database resources effectively. In this comprehensive guide, we will explore PostgreSQL roles and privileges, discussing how they work, how to create and manage them, and how they play a pivotal role in securing your database environment.

PostgreSQL Roles and Privileges

Understanding PostgreSQL Roles

What are PostgreSQL Roles?

In PostgreSQL, roles are a fundamental concept that plays a crucial role in access control.
A role is an entity that can be either a user or a group of users, and it defines specific permissions and access rights within the database.
Roles help in distinguishing between different types of users and governing their interactions with database objects.

Types of Roles

PostgreSQL supports two primary types of roles:

🙋‍ User Roles: These roles represent individual users and their access privileges. User roles can log in and perform actions within the database.

👥 Group Roles: Also known as "roles," group roles are collections of user roles or other group roles. They are useful for simplifying permission management and creating logical groupings of users.

Role Creation and Management

Creating Roles

To create a role in PostgreSQL, you can use the CREATE ROLE statement. For example:

CREATE ROLE my_user LOGIN PASSWORD 'my_password';

This statement creates a user role named "my_user" with a password. The LOGIN keyword allows the role to log in.

Assigning Privileges

Roles can be granted various privileges to interact with database objects. Privileges include SELECT, INSERT, UPDATE, DELETE, and many others. You can grant privileges using the GRANT statement. For instance:

GRANT SELECT, INSERT ON my_table TO my_user;

This grants the "my_user" role the ability to perform SELECT and INSERT operations on "my_table."

Role Hierarchy

PostgreSQL allows you to define role hierarchies, where one role can inherit privileges from another. This simplifies the management of permissions and roles. For example:

CREATE ROLE manager;
CREATE ROLE employee;
GRANT manager TO employee;

In this scenario, the "employee" role inherits the privileges of the "manager" role.

Controlling Access with Privileges

Privilege Levels

PostgreSQL provides several privilege levels, including:

  1. Database-level Privileges: These privileges apply to the entire database.

  2. Schema-level Privileges: Privileges can be granted at the schema level, allowing fine-grained control over specific schemas within a database.

  3. Table-level Privileges: For even more granularity, you can grant privileges at the table level, specifying which actions are allowed on individual tables.

Revoking Privileges

To remove privileges, you can use the REVOKE statement. For example:

REVOKE SELECT ON my_table FROM my_user;

This revokes the SELECT privilege from the "my_user" role on the "my_table."

Using the DEFAULT Privilege

You can also set default privileges for a role using the ALTER ROLE statement. Default privileges define what privileges are granted automatically when new objects are created within the schema.

Security Best Practices

Principle of Least Privilege

Adhering to the principle of least privilege is crucial in database security. Only grant the minimum necessary privileges to each role to reduce the potential for security breaches.

Regularly Review and Update Privileges

Database environments are dynamic. Regularly review and update role privileges to align with changing business needs and user roles.

Audit and Monitoring

Implement auditing and monitoring solutions to keep track of role activities and changes in access control. PostgreSQL provides tools for auditing and monitoring, such as logging and third-party extensions.

Frequently Asked Questions

Fixing role "postgres" does not exist

To check for the existence of this role, execute the following command within the PostgreSQL interactive shell using \du:


The relevant line appears as follows:

 Role name |            Attributes             | Member of 
 postgres  | Superuser, Create role, Create DB | {}        

Ensure that you have at least one role with superuser privileges; otherwise, you may encounter issues. If such a role exists, you can use it to log in. To verify permissions, examine the output of the \l command, which should match those of the superuser postgres on my Ubuntu system. If your setup uses the user as the superuser, you can attempt to log in using this command:

sudo -u user psql user

If user is indeed the database superuser, you can create another DB superuser and a private, empty database for them using the following commands:


Grant All Privileges in PostgreSQL

To grant all privileges on all tables in a specific schema to a user in PostgreSQL, you can use the GRANT command with the ALL PRIVILEGES keyword. Here's the SQL command to grant all privileges on all tables in a schema:


Here's what each part of this command does:

  • GRANT: This keyword indicates that you are granting privileges.
  • ALL PRIVILEGES: This specifies that you want to grant all available privileges, including SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER, and others.
  • ON ALL TABLES: This part of the command indicates that the privileges should apply to all tables in the specified schema.
  • IN SCHEMA your_schema: Replace 'your_schema' with the name of the schema where you want to grant the privileges.
  • TO your_user: Replace 'your_user' with the name of the user or role to whom you want to grant these privileges.

After executing this command, the specified user will have all privileges on all tables within the specified schema. Be cautious when granting such extensive privileges and ensure that you trust the user or role receiving them.


PostgreSQL roles and privileges are essential tools for managing access control in your database. By defining roles, granting and revoking privileges, and adhering to security best practices, you can ensure that your database remains secure and that users have the appropriate level of access to perform their tasks. Understanding these concepts is fundamental to maintaining the integrity and security of your PostgreSQL database, making it an invaluable skill for database administrators and developers alike.

Back to blog

Stop worrying about your backups.
Focus on building amazing things!

Free 7-day trial. No credit card required.

Have a question? Need help getting started?
Get in touch via chat or at [email protected]

Customer support with experts
Security & privacy first
Service that you'll love using