I know I can list my postgres databases with the following command:
# list only the databases
psql -U openerp -q -t -c "select datname from pg_database;" template1
Or
# show a full listing of databases
psql template1 -U openerp -l
How can I filter the databases by owner 'openerp'? datdba
field is just a number... how can I map it?
asked Jun 11, 2015 at 21:46
1 Answer 1
For things like this, you'll want to query pg_database
directly. The owner
field is a reference to pg_catalog.pg_authid.oid
, but this is a superuser-only accessible table. There's a view, pg_catalog.pg_roles
that exposes the id as the oid
attribute, so you can:
select
datname,
rolname AS dbowner
from pg_database d
inner join pg_roles r on (d.datdba = r.oid);
answered Jun 12, 2015 at 2:30
-
U're right. After digging for hours I found this approach (yes, i'm a pg n00b)Luis Masuelli– Luis Masuelli2015年06月14日 16:10:48 +00:00Commented Jun 14, 2015 at 16:10
-
It works! I just added a
where
clause to filter by my desired rolname.where rolname='ramy'
.Ramy Moussa– Ramy Moussa2020年09月16日 17:59:56 +00:00Commented Sep 16, 2020 at 17:59
lang-sql