Bird
Raised Fist0
PostgreSQLquery~5 mins

Role creation and management in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a role in PostgreSQL?
A role in PostgreSQL is an entity that can own database objects and have database privileges. It can represent a user or a group of users.
Click to reveal answer
beginner
How do you create a new role with login capability?
Use the command: CREATE ROLE role_name LOGIN; This creates a role that can be used to connect to the database.
Click to reveal answer
intermediate
What command grants a role the ability to create databases?
Use: ALTER ROLE role_name CREATEDB; to allow the role to create new databases.
Click to reveal answer
intermediate
How can you assign one role to another role (role membership)?
Use: GRANT role_name TO member_role; to make member_role inherit privileges of role_name.
Click to reveal answer
intermediate
How do you remove a role from the database?
Use: DROP ROLE role_name; to delete the role. Make sure the role owns no database objects or is not connected.
Click to reveal answer
Which command creates a role that can log in to the database?
ACREATE ROLE user1 NOCREATE;
BCREATE ROLE user1 LOGIN;
CALTER ROLE user1 LOGIN;
DGRANT LOGIN TO user1;
How do you allow a role to create new databases?
AGRANT CREATEDB TO role_name;
BCREATE ROLE role_name CREATEDB;
CALTER ROLE role_name CREATEDB;
DSET role_name CREATEDB;
What does the command GRANT admin TO user; do?
AGives user the privileges of admin role.
BCreates a new role named admin for user.
CRemoves admin role from user.
DChanges user role to admin.
Which command removes a role from PostgreSQL?
AREMOVE ROLE role_name;
BDROP USER role_name;
CDELETE ROLE role_name;
DDROP ROLE role_name;
Can a role in PostgreSQL represent a group of users?
AYes, roles can be groups or individual users.
BNo, roles are only individual users.
COnly if the role has LOGIN privilege.
DOnly if the role is a superuser.
Explain how to create a new PostgreSQL role that can log in and create databases.
Think about the commands to create a role and then grant it database creation rights.
You got /3 concepts.
    Describe how role membership works in PostgreSQL and how to grant one role to another.
    Consider how roles can be grouped by granting one role to another.
    You got /3 concepts.

      Practice

      (1/5)
      1. What is the primary purpose of a role in PostgreSQL?
      easy
      A. To control access and permissions for users and groups
      B. To store data in tables
      C. To create backups of the database
      D. To optimize query performance

      Solution

      1. Step 1: Understand the concept of roles

        Roles in PostgreSQL are used to manage who can access the database and what they can do.
      2. Step 2: Identify the main function of roles

        Roles control permissions and access rights, not data storage or backups.
      3. Final Answer:

        To control access and permissions for users and groups -> Option A
      4. Quick Check:

        Roles = Access control [OK]
      Hint: Roles manage user permissions and access rights [OK]
      Common Mistakes:
      • Confusing roles with tables or data storage
      • Thinking roles handle backups
      • Assuming roles optimize queries
      2. Which of the following is the correct syntax to create a role with login permission in PostgreSQL?
      easy
      A. CREATE ROLE user1 WITH LOGIN;
      B. CREATE ROLE user1 CAN LOGIN;
      C. CREATE ROLE user1 LOGIN;
      D. CREATE ROLE user1 ALLOW LOGIN;

      Solution

      1. Step 1: Recall the syntax for creating a role with login

        The correct syntax uses WITH LOGIN to allow the role to log in.
      2. Step 2: Check each option

        CREATE ROLE user1 WITH LOGIN; uses 'WITH LOGIN' which is correct. Others use incorrect keywords.
      3. Final Answer:

        CREATE ROLE user1 WITH LOGIN; -> Option A
      4. Quick Check:

        WITH LOGIN = enable login [OK]
      Hint: Use WITH LOGIN to grant login rights when creating roles [OK]
      Common Mistakes:
      • Omitting WITH before LOGIN
      • Using CAN or ALLOW instead of WITH
      • Forgetting semicolon at end
      3. Given the commands:
      CREATE ROLE analyst NOLOGIN;
      ALTER ROLE analyst CREATEDB;

      What is true about the role analyst?
      medium
      A. The role can log in but cannot create databases
      B. The role can log in and create databases
      C. The role cannot log in but can create databases
      D. The role cannot log in and cannot create databases

      Solution

      1. Step 1: Analyze the CREATE ROLE command

        The role 'analyst' is created with NOLOGIN, so it cannot log in.
      2. Step 2: Analyze the ALTER ROLE command

        The role is altered to have CREATEDB permission, so it can create databases.
      3. Final Answer:

        The role cannot log in but can create databases -> Option C
      4. Quick Check:

        NOLOGIN + CREATEDB = no login, can create DB [OK]
      Hint: NOLOGIN disables login; CREATEDB allows database creation [OK]
      Common Mistakes:
      • Assuming NOLOGIN means role cannot do anything
      • Confusing CREATEDB with login permission
      • Ignoring ALTER ROLE effects
      4. Identify the error in the following command:
      CREATE ROLE manager LOGIN PASSWORD 'secret';
      medium
      A. PASSWORD must be set using ENCRYPTED keyword
      B. LOGIN cannot be used with PASSWORD
      C. PASSWORD should be set with USING keyword
      D. PASSWORD must be set using WITH keyword

      Solution

      1. Step 1: Check correct syntax for setting password

        In PostgreSQL, options like PASSWORD must be specified after WITH keyword.
      2. Step 2: Identify the error in the command

        The command misses WITH before PASSWORD, causing syntax error.
      3. Final Answer:

        PASSWORD must be set using WITH keyword -> Option D
      4. Quick Check:

        Use WITH before PASSWORD [OK]
      Hint: Use WITH before PASSWORD when creating roles [OK]
      Common Mistakes:
      • Omitting WITH before PASSWORD
      • Thinking LOGIN disallows PASSWORD
      • Using ENCRYPTED incorrectly
      5. You want to create a role named developer that can log in, create databases, and also inherit permissions from another role team_member. Which command correctly achieves this?
      hard
      A. CREATE ROLE developer WITH LOGIN CREATEDB INHERIT team_member;
      B. CREATE ROLE developer WITH LOGIN CREATEDB INHERIT; GRANT team_member TO developer;
      C. CREATE ROLE developer WITH LOGIN CREATEDB INHERIT; ALTER ROLE developer IN ROLE team_member;
      D. CREATE ROLE developer WITH LOGIN CREATEDB INHERIT; GRANT developer TO team_member;

      Solution

      1. Step 1: Create role with login, createdb, and inherit

        The role must be created with WITH LOGIN, CREATEDB, and INHERIT options.
      2. Step 2: Grant membership to inherit permissions

        To inherit permissions from team_member, grant team_member role to developer using GRANT.
      3. Step 3: Check each option

        CREATE ROLE developer WITH LOGIN CREATEDB INHERIT; GRANT team_member TO developer; correctly creates the role and grants team_member to developer. Others misuse syntax or reverse grant direction.
      4. Final Answer:

        CREATE ROLE developer WITH LOGIN CREATEDB INHERIT; GRANT team_member TO developer; -> Option B
      5. Quick Check:

        GRANT role TO user for inheritance [OK]
      Hint: Use GRANT role TO user to inherit permissions [OK]
      Common Mistakes:
      • Putting role name after INHERIT
      • Reversing GRANT direction
      • Missing WITH keyword or semicolon