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
| Attribute | Description |
|---|---|
| LOGIN | Can connect |
| SUPERUSER | Full access |
| CREATEDB | Create databases |
| CREATEROLE | Create roles |
| REPLICATION | Replication |
List Roles
\du
SELECT rolname FROM pg_roles;
Drop Role
DROP ROLE myuser;
Mini Practice
- Create a role
- Grant privileges
- Revoke privileges
- 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.