DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

PostgreSQL. How to get DBs that a given user can access?

select pgu.usename as user_name,
(select string_agg(pgd.datname, ',' order by pgd.datname)
from pg_database pgd
where has_database_privilege(pgu.usename, pgd.datname,
'CONNECT')) as database_name
from pg_user pgu
where pgu.usename = 'some_username'
order by pgu.usename;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)