pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
how to switch user in postgres
4+ messages / 4 participants
[nested] [flat]

* how to switch user in postgres
@ 2026-04-11 17:55 ikramuddin <ikram.amani815@gmail.com>
  2026-04-11 18:00 ` Re: how to switch user in postgres Pavel Stehule <pavel.stehule@gmail.com>
  2026-04-11 18:33 ` Re: how to switch user in postgres David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: ikramuddin @ 2026-04-11 17:55 UTC (permalink / raw)
  To: pgsql-performance@lists.postgresql.org

hi team,
As i was working through postgresql user and role privileges i faced an
unexpected behavious when we revoke connect on database from a user then
why still we need to revoke usage on schema basis and then revoke select ,
insert , update privileges at table table. second when i did all this one
by one and then tried to connect to same database using \c database_name
user_name, it failed that is perfect which i expected but when i tried to
connect through postgres as a superuser it connect still it is fine. but
then i write the command set role simon it connected even i revoked the
connect privileges from the role . plz guide it is a bug or this
behavious left intentionally.
finance=> SET ROLE postgres;
SET
finance=# REVOKE CONNECT ON DATABASE finance FROM simon;
REVOKE
finance=# set role simon;
SET
finance=> SELECT * FROM accounting.invoices;
 invoice_id | invoice_date | amount
------------+--------------+--------
          1 | 2024-03-15   | 250.50
          2 | 2024-01-20   | 110.99
          3 | 2024-03-29   |   1000
(3 rows)

finance=> SET ROLE postgres;
SET
finance=# REVOKE CONNECT ON DATABASE finance FROM simon; REVOKE USAGE ON
SCHEMA accounting FROM simon; REVOKE SELECT, INSERT, UPDATE, DELETE ON
accounting.invoices FROM simon;
REVOKE
REVOKE
REVOKE
finance=# CREATE ROLE accounting_ro NOLOGIN; GRANT CONNECT ON DATABASE
finance TO accounting_ro; GRANT USAGE ON SCHEMA accounting TO
accounting_ro; GRANT SELECT ON ALL TABLES IN SCHEMA accounting TO
accounting_ro;
CREATE ROLE
GRANT
GRANT
GRANT
finance=# CREATE TABLE accounting.customers(   customer_id serial PRIMARY
KEY,   name TEXT,   address TEXT );
CREATE TABLE
finance=# SET ROLE simon;
SET
finance=> select current_user;
 current_user
--------------
 simon
(1 row)

finance=> reset role'
finance'> ;
finance'> ';
ERROR:  syntax error at or near "'
;
'"
LINE 1: reset role'
                  ^
finance=> reset role;
RESET
finance=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype
   | Locale | ICU Rules |    Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+--------------------------
 finance   | postgres | UTF8     | libc            | en_US.UTF-8 |
en_US.UTF-8 |        |           | =Tc/postgres            +
           |          |          |                 |             |
    |        |           | postgres=CTc/postgres   +
           |          |          |                 |             |
    |        |           | accounting_ro=c/postgres
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 |
en_US.UTF-8 |        |           |
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 |
en_US.UTF-8 |        |           | =c/postgres             +
           |          |          |                 |             |
    |        |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 |
en_US.UTF-8 |        |           | =c/postgres             +
           |          |          |                 |             |
    |        |           | postgres=CTc/postgres
(4 rows)

finance=# revoke connect on database finance from simon;
REVOKE
finance=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=# revoke connect on database finance from simon;
REVOKE
postgres=# \c finance simon;
connection to server on socket "/run/postgresql/.s.PGSQL.5432" failed:
FATAL:  Peer authentication failed for user "simon"
Previous connection kept
postgres=# \c finance
You are now connected to database "finance" as user "postgres".
finance=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=# \c finance postgres
You are now connected to database "finance" as user "postgres".
finance=#
finance=# set role simon
finance-# ;
SET
finance=> select current_role;
 current_role
--------------
 simon
(1 row)

finance=> SELECT * FROM accounting.invoices;
ERROR:  permission denied for schema accounting
LINE 1: SELECT * FROM accounting.invoices;
                      ^
finance=> ^C

thanks and regards
Ikramuddin Database lead.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: how to switch user in postgres
  2026-04-11 17:55 how to switch user in postgres ikramuddin <ikram.amani815@gmail.com>
@ 2026-04-11 18:00 ` Pavel Stehule <pavel.stehule@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Pavel Stehule @ 2026-04-11 18:00 UTC (permalink / raw)
  To: ikramuddin <ikram.amani815@gmail.com>; +Cc: pgsql-performance@lists.postgresql.org

Hi

so 11. 4. 2026 v 19:55 odesílatel ikramuddin <ikram.amani815@gmail.com>
napsal:

> hi team,
> As i was working through postgresql user and role privileges i faced an
> unexpected behavious when we revoke connect on database from a user then
> why still we need to revoke usage on schema basis and then revoke select ,
> insert , update privileges at table table. second when i did all this one
> by one and then tried to connect to same database using \c database_name
> user_name, it failed that is perfect which i expected but when i tried to
> connect through postgres as a superuser it connect still it is fine. but
> then i write the command set role simon it connected even i revoked the
> connect privileges from the role . plz guide it is a bug or this
> behavious left intentionally.
> finance=> SET ROLE postgres;
> SET
> finance=# REVOKE CONNECT ON DATABASE finance FROM simon;
> REVOKE
> finance=# set role simon;
> SET
> finance=> SELECT * FROM accounting.invoices;
>  invoice_id | invoice_date | amount
> ------------+--------------+--------
>           1 | 2024-03-15   | 250.50
>           2 | 2024-01-20   | 110.99
>           3 | 2024-03-29   |   1000
> (3 rows)
>
> finance=> SET ROLE postgres;
> SET
> finance=# REVOKE CONNECT ON DATABASE finance FROM simon; REVOKE USAGE ON
> SCHEMA accounting FROM simon; REVOKE SELECT, INSERT, UPDATE, DELETE ON
> accounting.invoices FROM simon;
> REVOKE
> REVOKE
> REVOKE
> finance=# CREATE ROLE accounting_ro NOLOGIN; GRANT CONNECT ON DATABASE
> finance TO accounting_ro; GRANT USAGE ON SCHEMA accounting TO
> accounting_ro; GRANT SELECT ON ALL TABLES IN SCHEMA accounting TO
> accounting_ro;
> CREATE ROLE
> GRANT
> GRANT
> GRANT
> finance=# CREATE TABLE accounting.customers(   customer_id serial PRIMARY
> KEY,   name TEXT,   address TEXT );
> CREATE TABLE
> finance=# SET ROLE simon;
> SET
> finance=> select current_user;
>  current_user
> --------------
>  simon
> (1 row)
>
> finance=> reset role'
> finance'> ;
> finance'> ';
> ERROR:  syntax error at or near "'
>

there is no RESET role command

you can use `SET ROLE TO DEFAULT` instead

Regards

Pavel

;
> '"
> LINE 1: reset role'
>                   ^
> finance=> reset role;
> RESET
> finance=# \l
>                                                        List of databases
>    Name    |  Owner   | Encoding | Locale Provider |   Collate   |
>  Ctype    | Locale | ICU Rules |    Access privileges
>
> -----------+----------+----------+-----------------+-------------+-------------+--------+-----------+--------------------------
>  finance   | postgres | UTF8     | libc            | en_US.UTF-8 |
> en_US.UTF-8 |        |           | =Tc/postgres            +
>            |          |          |                 |             |
>     |        |           | postgres=CTc/postgres   +
>            |          |          |                 |             |
>     |        |           | accounting_ro=c/postgres
>  postgres  | postgres | UTF8     | libc            | en_US.UTF-8 |
> en_US.UTF-8 |        |           |
>  template0 | postgres | UTF8     | libc            | en_US.UTF-8 |
> en_US.UTF-8 |        |           | =c/postgres             +
>            |          |          |                 |             |
>     |        |           | postgres=CTc/postgres
>  template1 | postgres | UTF8     | libc            | en_US.UTF-8 |
> en_US.UTF-8 |        |           | =c/postgres             +
>            |          |          |                 |             |
>     |        |           | postgres=CTc/postgres
> (4 rows)
>
> finance=# revoke connect on database finance from simon;
> REVOKE
> finance=# \c postgres
> You are now connected to database "postgres" as user "postgres".
> postgres=# revoke connect on database finance from simon;
> REVOKE
> postgres=# \c finance simon;
> connection to server on socket "/run/postgresql/.s.PGSQL.5432" failed:
> FATAL:  Peer authentication failed for user "simon"
> Previous connection kept
> postgres=# \c finance
> You are now connected to database "finance" as user "postgres".
> finance=# \c postgres
> You are now connected to database "postgres" as user "postgres".
> postgres=# \c finance postgres
> You are now connected to database "finance" as user "postgres".
> finance=#
> finance=# set role simon
> finance-# ;
> SET
> finance=> select current_role;
>  current_role
> --------------
>  simon
> (1 row)
>
> finance=> SELECT * FROM accounting.invoices;
> ERROR:  permission denied for schema accounting
> LINE 1: SELECT * FROM accounting.invoices;
>                       ^
> finance=> ^C
>
> thanks and regards
> Ikramuddin Database lead.
>

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: how to switch user in postgres
  2026-04-11 17:55 how to switch user in postgres ikramuddin <ikram.amani815@gmail.com>
@ 2026-04-11 18:33 ` David G. Johnston <david.g.johnston@gmail.com>
  2026-04-11 19:29   ` Re: how to switch user in postgres Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 4+ messages in thread

From: David G. Johnston @ 2026-04-11 18:33 UTC (permalink / raw)
  To: ikramuddin <ikram.amani815@gmail.com>; +Cc: pgsql-performance@lists.postgresql.org

This seems quite misplaced on the -performance mailing list.

On Sat, Apr 11, 2026 at 10:55 AM ikramuddin <ikram.amani815@gmail.com>
wrote:

>
> As i was working through postgresql user and role privileges i faced an
> unexpected behavious when we revoke connect on database from a user then
> why still we need to revoke usage on schema basis and then revoke select ,
> insert , update privileges at table table. second when i did all this one
> by one and then tried to connect to same database using \c database_name
> user_name, it failed that is perfect which i expected but when i tried to
> connect through postgres as a superuser it connect still it is fine. but
> then i write the command set role simon it connected even i revoked the
> connect privileges from the role .
>


> plz guide it is a bug or this behavious left intentionally.
>

Intentional, every object has its own permissions that are granted to roles
independently of others.


> finance=> reset role'
> finance'> ;
> finance'> ';
> ERROR:  syntax error at or near "'
> ;
> '"
> LINE 1: reset role'
>
                  ^
> finance=> reset role;
> RESET
>

You had a typo in the first attempt...

For everything else I really don't feel like trying to read your mind.  If
you have questions about specific outcomes or whatnot please ask them
directly.  But there are no known bugs here and I presume you haven't
demonstrated any - everything is working as intended.

David J.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: how to switch user in postgres
  2026-04-11 17:55 how to switch user in postgres ikramuddin <ikram.amani815@gmail.com>
  2026-04-11 18:33 ` Re: how to switch user in postgres David G. Johnston <david.g.johnston@gmail.com>
@ 2026-04-11 19:29   ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2026-04-11 19:29 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: ikramuddin <ikram.amani815@gmail.com>; pgsql-performance@lists.postgresql.org

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> This seems quite misplaced on the -performance mailing list.

Indeed.

> On Sat, Apr 11, 2026 at 10:55 AM ikramuddin <ikram.amani815@gmail.com>
> wrote:
>> plz guide it is a bug or this behavious left intentionally.

> Intentional, every object has its own permissions that are granted to roles
> independently of others.

Also, "REVOKE CONNECT ON DATABASE finance FROM simon" is probably a
no-op, because nobody ever did "GRANT CONNECT ON DATABASE finance TO
simon".  Rather, the reason simon can connect is that there's a
default "GRANT CONNECT ... TO public".  If you want to restrict
CONNECT privileges, you have to revoke that and then hand out
the privilege selectively to users that should have it.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2026-04-11 19:29 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-04-11 17:55 how to switch user in postgres ikramuddin <ikram.amani815@gmail.com>
2026-04-11 18:00 ` Pavel Stehule <pavel.stehule@gmail.com>
2026-04-11 18:33 ` David G. Johnston <david.g.johnston@gmail.com>
2026-04-11 19:29   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox