Bird
Raised Fist0
PostgreSQLquery~20 mins

Role creation and management in PostgreSQL - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Role Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this role creation query?
Consider the following SQL command executed in PostgreSQL:

CREATE ROLE analyst LOGIN PASSWORD 'secure123' NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT;

What will be the result when you run \du analyst to list this role's attributes?
PostgreSQL
CREATE ROLE analyst LOGIN PASSWORD 'secure123' NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT;
ARole name: analyst, Attributes: Cannot create DB, Can create role, Can inherit, Can login
BRole name: analyst, Attributes: Superuser, Can create DB, Can create role, Can inherit, Can login
CRole name: analyst, Attributes: Cannot login, Can create DB, Can create role, Can inherit
DRole name: analyst, Attributes: Cannot create DB, Cannot create role, Cannot inherit, Can login
Attempts:
2 left
💡 Hint
Look carefully at the keywords NOSUPERUSER, NOCREATEDB, NOCREATEROLE, NOINHERIT and LOGIN.
📝 Syntax
intermediate
1:30remaining
Which option correctly grants a role the ability to create databases?
You want to grant the role 'developer' the permission to create databases. Which of the following SQL commands is syntactically correct and achieves this?
AALTER ROLE developer WITH CREATEDB;
BALTER ROLE developer SET CREATEDB;
CALTER ROLE developer CREATEDB;
DALTER ROLE developer GRANT CREATEDB;
Attempts:
2 left
💡 Hint
Remember the syntax for altering role attributes in PostgreSQL.
🔧 Debug
advanced
2:00remaining
Why does this role creation command fail?
You run the following command:

CREATE ROLE manager PASSWORD 'pass123' LOGIN;

But PostgreSQL returns an error. What is the cause?
PostgreSQL
CREATE ROLE manager PASSWORD 'pass123' LOGIN;
AThe PASSWORD clause is deprecated; use ENCRYPTED PASSWORD instead.
BThe PASSWORD clause must come after LOGIN keyword.
CThe LOGIN keyword must come before PASSWORD clause.
DThe PASSWORD clause requires the keyword ENCRYPTED before it.
Attempts:
2 left
💡 Hint
Check PostgreSQL documentation for role creation syntax regarding passwords.
🧠 Conceptual
advanced
1:30remaining
What is the effect of the NOINHERIT attribute on a role?
In PostgreSQL, what does setting the NOINHERIT attribute on a role do?
AThe role cannot grant privileges to other roles.
BThe role cannot inherit privileges from roles it is a member of.
CThe role cannot login to the database.
DThe role cannot be a member of any other role.
Attempts:
2 left
💡 Hint
Think about what inheritance means in role membership.
optimization
expert
2:30remaining
Efficiently revoking multiple privileges from a role
You want to revoke the privileges to create databases and create roles from the role 'temp_admin'. Which single SQL command correctly and efficiently revokes both privileges?
AALTER ROLE temp_admin NOCREATEDB NOCREATEROLE;
BREVOKE CREATEDB, CREATEROLE FROM temp_admin;
CREVOKE ALL PRIVILEGES ON DATABASE temp_admin;
DALTER ROLE temp_admin REVOKE CREATEDB, CREATEROLE;
Attempts:
2 left
💡 Hint
Remember that CREATEDB and CREATEROLE are role attributes, not privileges on objects.

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