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:
filmactorcustomer
๐ DO NOT change owner automatically
๐น Step 4: Verify Ownership
In terminal:
\lExpected Output:
| Database | Owner |
|---|---|
| dvdrental | senior_admin |
โ ๏ธ Permissions Requirement
Only:
- Superuser
- OR current database owner
๐ can run ALTER DATABASE
Otherwise:
โ permission denied