Lesson 2of 8

CREATE ROLE in PostgreSQL | User Management & Permissions Guide

Learn how to create roles and users in PostgreSQL with login privileges, passwords, and permission management. Includes practical examples and CREATE ROLE syntax.


In PostgreSQL, a role is a central concept for managing users and permissions.


๐Ÿ”น What is a Role?

A role is an entity that can:

  • Own database objects (tables, views, etc.)
  • Have permissions (SELECT, INSERT, etc.)
  • Login into the database (if allowed)

๐Ÿ‘‰ In simple words: Role = User + Permissions system


๐Ÿ”น Types of Roles

There are mainly two types:

1. Login Role (User)

  • Can log in to the database
  • Example: student1, admin

2. Group Role

  • Cannot log in
  • Used to assign permissions to multiple users

๐Ÿ”น Why We Use Roles?

Roles are used for security and management:

โœ… 1. Access Control

  • Restrict who can access data

โœ… 2. Permission Management

  • Assign permissions once to a role, then assign role to many users

โœ… 3. Better Organization

  • Separate admin, teacher, student roles

โœ… 4. Data Security

  • Prevent unauthorized changes

๐Ÿ”น Syntax: Create Role

Basic Syntax:

CREATE ROLE role_name;

๐Ÿ”น Create Role with Login (User)

CREATE ROLE student1
WITH LOGIN
PASSWORD '1234';

๐Ÿ”น Create Role with Common Options

CREATE ROLE teacher
WITH 
LOGIN
PASSWORD 'securepass'
CREATEDB
CREATEROLE;

๐Ÿ”น Important Options

Option Meaning
LOGIN Can log in (makes it a user)
PASSWORD Set password
CREATEDB Can create databases
CREATEROLE Can create roles
SUPERUSER Full access (admin)
NOSUPERUSER Normal user

๐Ÿ”น Example

CREATE ROLE admin_user
WITH LOGIN
PASSWORD 'admin123'
SUPERUSER;

๐Ÿ”น Grant Role to Another User

GRANT teacher TO student1;

๐Ÿงช Lab: Understanding Roles using dvdrental Database

๐ŸŽฏ Learning Outcomes

Students will:

  • Understand what a role is
  • Create roles with different privileges
  • View and analyze roles
  • Change database ownership
  • Understand ownership vs permissions

๐Ÿ”น Step 1: Create a New Role (User)

Before assigning ownership, the role must exist.

CREATE ROLE senior_admin 
WITH LOGIN 
PASSWORD 'secure_pass123';

๐Ÿง  Concept:

  • LOGIN โ†’ makes it a user
  • Without LOGIN โ†’ it is just a group role

๐Ÿ‘‰ Key Idea: In PostgreSQL, there is no separate "user" concept โ€” everything is a role


๐Ÿ”น Step 2: View All Roles

SELECT rolname FROM pg_roles;

โœ”๏ธ This retrieves all roles in the server.


๐Ÿ” Understanding pg_roles

  • It is a system catalog view

  • Stores:

    • Users
    • Groups
    • Privileges
    • Role attributes

๐Ÿ“Š Better Query (Detailed View)

SELECT 
    rolname, 
    rolsuper, 
    rolcreaterole, 
    rolcreatedb, 
    rolcanlogin
FROM pg_roles;

๐Ÿง  Column Understanding:

Column Meaning
rolname Role name
rolsuper Superuser or not
rolcreaterole Can create roles
rolcreatedb Can create DB
rolcanlogin Can login

๐ŸŽฏ Filter Only Users

SELECT rolname
FROM pg_roles
WHERE rolcanlogin = true;

๐Ÿ–ฅ๏ธ Alternative (psql)

\du

๐Ÿ”น Step 3: Assign Ownership of dvdrental

ALTER DATABASE dvdrental OWNER TO senior_admin;

๐Ÿง  Concept: Ownership

Ownership is very powerful:

  • Owner can:

    • Drop database
    • Rename database
    • Grant permissions

๐Ÿ‘‰ Ownership โ‰  Normal permissions


โ— Database Ownership vs Table Ownership

After running:

ALTER DATABASE dvdrental OWNER TO senior_admin;

โœ”๏ธ Only the database owner changes

โŒ Tables like:

  • film
  • actor
  • customer

๐Ÿ‘‰ DO NOT change owner automatically


๐Ÿ”น Step 4: Verify Ownership

In terminal:

\l

Expected Output:

Database Owner
dvdrental senior_admin

โš ๏ธ Permissions Requirement

Only:

  • Superuser
  • OR current database owner

๐Ÿ‘‰ can run ALTER DATABASE

Otherwise: โŒ permission denied