PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - CREATE ROLE

In PostgreSQL, roles are a crucial concept, serving as a generalized way to handle database access permissions. A role can represent a database user, a group of database users, or both. Roles can own database objects (like tables) and can assign privileges on those objects to other roles to control who has access to which objects.

The CREATE ROLE SQL command is used to create a new role.

Syntax:

CREATE ROLE role_name [WITH option1 [ ... ] ];

The possible options include:

  • LOGIN: Allows the role to be used for logging in. Essentially, it turns the role into a user.
  • SUPERUSER: Grants superuser attributes to the role.
  • CREATEDB: Allows the role to create databases.
  • CREATEROLE: Allows the role to create other roles.
  • INHERIT: Role inherits privileges from roles it's a member of (this is the default).
  • NOSUPERUSER, NOCREATEDB, NOCREATEROLE, NOINHERIT: The inverse of the options above.
  • PASSWORD 'password': Sets the role's password.
  • VALID UNTIL 'timestamp': The role's password will expire at the provided timestamp.
  • REPLICATION: Gives the role the ability to initiate streaming replication or backups.

Examples:

  1. Create a simple role:

    CREATE ROLE myrole;
    
  2. Create a role with login and a password:

    CREATE ROLE myuser WITH LOGIN PASSWORD 'mypassword';
    
  3. Create a superuser role with login and a password:

    CREATE ROLE admin WITH LOGIN PASSWORD 'adminpass' SUPERUSER;
    
  4. Create a role that can create databases:

    CREATE ROLE dbcreator WITH LOGIN PASSWORD 'creatorpass' CREATEDB;
    
  5. Create a role with an expiration date for the password:

    CREATE ROLE tempuser WITH LOGIN PASSWORD 'temppass' VALID UNTIL '2023-12-31';
    

Notes:

  • It's essential to use strong passwords, especially for roles with elevated privileges.

  • Roles without the LOGIN attribute are useful for grouping other roles. You can grant or revoke privileges to such a group role, and all member roles will inherit the privileges.

  • Role management, in combination with PostgreSQL's robust permission system, allows for intricate and finely-grained control over who can access and modify data and structures within the database.

  • Always be careful when assigning the SUPERUSER attribute, as superusers can bypass all permission checks in PostgreSQL.

For handling roles and permissions, consider leveraging tools like pgAdmin or other PostgreSQL management tools, which can make visualizing and managing roles more intuitive.

  1. Creating user roles in PostgreSQL:

    • Description: Creates a new user role.
    • Code:
      CREATE ROLE your_role;
      
  2. Assigning privileges and permissions with CREATE ROLE in PostgreSQL:

    • Description: Grants specific privileges and permissions to the user role.
    • Code:
      CREATE ROLE your_role WITH LOGIN PASSWORD 'your_password';
      
  3. Setting role attributes in PostgreSQL:

    • Description: Configures various attributes for the user role.
    • Code:
      CREATE ROLE your_role WITH LOGIN PASSWORD 'your_password' CREATEDB CREATEROLE;
      
  4. Granting and revoking roles in PostgreSQL:

    • Description: Grants or revokes membership of a role to another role or user.
    • Code:
      GRANT your_role TO another_role;
      REVOKE your_role FROM another_role;
      
  5. Creating roles with LOGIN and NOLOGIN options in PostgreSQL:

    • Description: Specifies whether the role can log in or not.
    • Code:
      CREATE ROLE your_role WITH LOGIN PASSWORD 'your_password';
      CREATE ROLE your_role NOLOGIN;
      
  6. Managing password and authentication for roles in PostgreSQL:

    • Description: Sets or changes the password for a user role.
    • Code:
      ALTER ROLE your_role WITH PASSWORD 'new_password';
      
  7. Dropping roles with DROP ROLE in PostgreSQL:

    • Description: Removes a user role from the database.
    • Code:
      DROP ROLE your_role;
      
  8. Role inheritance and hierarchy in PostgreSQL:

    • Description: Defines a hierarchy of roles where a role inherits privileges from another.
    • Code:
      CREATE ROLE parent_role;
      CREATE ROLE child_role WITH INHERIT;
      GRANT parent_role TO child_role;