complete-tutorial-on-roles-and-permissions-in-postgresql
complete-tutorial-on-roles-and-permissions-in-postgresql

Complete tutorial on roles and permissions in PostgreSQL

In this article, you will learn in a complete and practical way how to create roles, grant permissions, manage permissions, create group roles, and control access on PostgreSQL.
0 Shares
0
0
0
0

Introduction

PostgreSQL is a powerful, enterprise-grade database that has an advanced system for managing users and access levels. In PostgreSQL, there is no such thing as a “User” in the traditional sense; everything is a single entity. Role which can have the behavior of a user or a group.
Proper use of roles and permissions increases security, better manages databases, and prevents unwanted access—especially when running PostgreSQL on a VPS.


1. Prerequisites for working with Roles and Permissions

To get started, you must have the following:

  • A Linux VPS (like Ubuntu 20.04 or 22.04)

  • PostgreSQL installed

  • Root or sudo user access

  • Basic introduction to the command line

Log in to the postgres account:

sudo -i -u postgres psql

2. Viewing Roles in PostgreSQL

To view all available roles:

\du

Sample output:

Role name | Attributes | Member of -----------+---------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replicate | {}

3. Create a new role

Method 1: Create a role from within psql

CREATE ROLE user_demo;

This role does not yet have login permission.

Method 2: Creating a role with specific features

Example: Role that can be logged in with a password:

CREATE ROLE user_demo WITH LOGIN PASSWORD 'StrongPass123';

Method 3: Create a role via CLI

createuser --interactive

In this method, you will be asked:

  • Is it a superuser?

  • Is it allowed to create a database?

  • Is it allowed to create Roles?


4. Delete a role in PostgreSQL

Delete role:

DROP ROLE user_demo;

To avoid an error if the role does not exist:

DROP ROLE IF EXISTS user_demo;

5. Settings when creating a Role

PostgreSQL allows you to specify important properties when creating a Role:

CREATE ROLE app_user WITH LOGIN CREATEDB NOCREATEROLE PASSWORD 'ExamplePass';

The most important attributes:

FeatureExplanation
LOGINPermission to access the database
CREATEDBPermission to create database
CREATEROLEPermission to create a role
INHERITInheriting permission from the role of mother
SUPERUSERFull access
PASSWORDPassword
VALID UNTILSet expiration date

6. Change Role settings with ALTER ROLE

If you want to change a role's permissions later:

Adding Login:

ALTER ROLE app_user WITH LOGIN;

Remove the ability to Login:

ALTER ROLE app_user WITH NOLOGIN;

Change password:

ALTER ROLE app_user WITH PASSWORD 'NewPassword!';

7. Log in to PostgreSQL with another role

1) First set the role password:

\password app_user

2) Exit psql:

\q

3) Login as a new role:

psql -U app_user -d postgres -h 127.0.0.1 -W

8. Create a table to test permissions

CREATE TABLE employees ( id serial PRIMARY KEY, name varchar(50), created_at timestamp DEFAULT NOW() );

9. Granting Permissions (GRANT) in PostgreSQL

Granting a specific permission:

GRANT SELECT ON employees TO app_user;

INSERT permission:

GRANT INSERT ON employees TO app_user;

Giving ALL Privileges:

GRANT ALL ON employees TO app_user;

Giving access to all users (Public):

GRANT SELECT ON employees TO PUBLIC;

10. View permissions for each table

\z employees

11. Revoke permissions (REVOKE)

Revoke Insert permission from a specific role:

REVOKE INSERT ON employees FROM app_user;

Revoke all permissions:

REVOKE ALL ON employees FROM app_user;

Remove general permission:

REVOKE SELECT ON employees FROM PUBLIC;

12. Group Role in PostgreSQL

A group role is a role that other roles are members of—like Linux groups.

1) Create a Group Role:

CREATE ROLE dev_team;

2) Adding users to the group:

GRANT dev_team TO app_user; GRANT dev_team TO backend_user;

3) Membership Check:

\du

4) Using the group role:

SET ROLE dev_team;

From this moment on, all permissions that dev_team has will be activated.

5) Automatic inheritance with INHERIT

ALTER ROLE app_user WITH INHERIT;

13. Transfer ownership of objects before deleting a role

If you want to delete a role but the table or database belongs to it:

Change table owner:

ALTER TABLE employees OWNER TO postgres;

Then delete the role:

DROP ROLE dev_team;

14. Important Security Tips When Using Roles on VPS

  • Each service (Backend, Cronjob, Worker) must have a separate Role.

  • As a security tip, use low-access roles.

  • In database architecture, no application has a role. superuser Don't give.

  • Role passwords must be strong and have an expiration date.

  • For example, always use groups to manage access.


Conclusion

Practically everything in PostgreSQL is Role-based. You can create users, groups, system roles, and application roles with a single administrative model. Also, tools GRANT and REVOKE They provide complete control over tables, schemas, databases, and functions.

Proper role management in PostgreSQL is very important, especially on a VPS, because the security and stability of your entire database system depends on it.

[Total: 0   Average: 0/5]
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like