</>
Skip to content
PostgreSQL lessons (35/38)

PostgreSQL — Roles

Create Role

CREATE ROLE myuser WITH LOGIN PASSWORD 'password';

Create User

CREATE USER myuser WITH PASSWORD 'password';

Grant Privileges

-- Grant database
GRANT CONNECT ON DATABASE mydb TO myuser;

-- Grant schema
GRANT USAGE ON SCHEMA public TO myuser;

-- Grant table
GRANT SELECT, INSERT, UPDATE ON users TO myuser;

Revoke Privileges

REVOKE INSERT ON users FROM myuser;

Role Attributes

AttributeDescription
LOGINCan connect
SUPERUSERFull access
CREATEDBCreate databases
CREATEROLECreate roles
REPLICATIONReplication

List Roles

\du
SELECT rolname FROM pg_roles;

Drop Role

DROP ROLE myuser;

Mini Practice

  1. Create a role
  2. Grant privileges
  3. Revoke privileges
  4. List all roles

Up Next

Continue with Permissions — permission management.

Related Topics

Frequently Asked Questions about Roles

What is Roles in PostgreSQL?

Roles is a fundamental concept in PostgreSQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Roles?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Roles.

Why is Roles important in PostgreSQL?

Roles is essential for PostgreSQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.