PostgreSQL Users/Roles and Privilages

Roles:

                  PostgreSQL manages database access permissions using the concept of roles. A role can be thought of as either a database user, or a group of database users, depending on how the role is set up. 
                 Roles can own database objects (for example, tables and functions) and can assign privileges on those objects to other roles to control who has access to which objects.
                 Furthermore, it is possible to grant membership in a role to another role, thus allowing the member role to use privileges assigned to another role.

Role Attributes:

CREATEDB         NOCREATEDB
CREATEROLE NOCREATEROLE
LOGIN           NOLOGIN
SUPERUSER          NOSUPERUSER
INHERIT NOINHERIT
BYPASSRLS         NOBYPASSRLS
REPLICATION NOREPLICATION
PASSWORD
ENCRYPTED PASSWORD
CONNECTION LIMIT
VALID UNTIL

Examples:

postgres=# \du+
                                          List of roles
 Role name |                         Attributes                         | Member of | Description
-----------+------------------------------------------------------------+-----------+-------------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}        |


postgres=# \l
                                                                                   List of databases
      Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                                                                                                      | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                                                                                                     | postgres=CTc/postgres
(3 rows)

postgres=# create role role_dba with superuser createdb createrole replication bypassrls inherit;
CREATE ROLE
postgres=# create user dba with password 'dba';
CREATE ROLE
postgres=# \du+
                                                 List of roles
 Role name |                                Attributes                                | Member of | Description
-----------+--------------------------------------------------------------------------+-----------+-------------
 dba       |                                                                          | {}        |
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS               | {}        |
 role_dba  | Superuser, Create role, Create DB, Cannot login, Replication, Bypass RLS | {}        |

How to assign Role to User:

postgres=# grant role_dba to dba;
GRANT ROLE
postgres=# \du+
                                                  List of roles
 Role name |                                Attributes                                | Member of  | Description
-----------+--------------------------------------------------------------------------+------------+-------------
 dba       |                                                                          | {role_dba} |
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS               | {}         |
 role_dba  | Superuser, Create role, Create DB, Cannot login, Replication, Bypass RLS | {}         |
 

How to grant access to users in PostgreSQL?

Here are some common statement to grant access to a PostgreSQL user:

Grant CONNECT to the database:

GRANT CONNECT ON DATABASE database_name TO username;

Grant USAGE on schema:

GRANT USAGE ON SCHEMA schema_name TO username;

Grant on all tables for DML statements: SELECT, INSERT, UPDATE, DELETE:

GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA schema_name TO username;

Grant all privileges on all tables in the schema:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schema_name TO username;

Grant all privileges on all sequences in the schema:

GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA schema_name TO username;

Grant all privileges on the database:

GRANT ALL PRIVILEGES ON DATABASE database_name TO username;

Grant permission to create database:

ALTER USER username CREATEDB;

Make a user superuser:

ALTER USER myuser WITH SUPERUSER;

Remove superuser status:

ALTER USER username WITH NOSUPERUSER;

Those statements above only affect the current existing tables. To apply to newly created tables, you need to use alter default. For example:

ALTER DEFAULT PRIVILEGES
FOR USER username
IN SCHEMA schema_name
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO username;

How to show particular user table privileges:


postgres=# select table_schema,table_name,privilege_type from information_schema.role_table_grants where grantee='u2';

 table_schema | table_name | privilege_type
--------------+------------+----------------
 public       | customers  | SELECT
 public       | customers  | UPDATE


Comments

Popular posts from this blog

PostgreSQL pg_pool-II Installation and Configuration

PostgreSQL Pages and Tuples

PostgreSQL Reporting Tools(Pgbadger) Installation & Configuration