Get DB owner's name in PostgreSql

Multi tool use
Get DB owner's name in PostgreSql
I have DB "test" in PostgreSql. I want to write sql to get owner my database.I searched Google
but answer not found.Please help me
5 Answers
5
You can find such things in the system catalog
SELECT d.datname as "Name",
pg_catalog.pg_get_userbyid(d.datdba) as "Owner"
FROM pg_catalog.pg_database d
WHERE d.datname = 'your_name'
ORDER BY 1;
You can use the combination of pg_database
, pg_users
system tables and current_database()
function in this way:
pg_database
pg_users
current_database()
SELECT u.usename
FROM pg_database d
JOIN pg_user u ON (d.datdba = u.usesysid)
WHERE d.datname = (SELECT current_database());
If you use the psql command-line tool, you can simply use l
l
This returns a list of databases, but the owners of the databases.
– Flimm
Mar 16 '17 at 17:56
@Flimm I didn't understand your comment - this returns a list of all existing DBs on the server (that current user has permission to view) with the owner of each DB.
– gilad mayani
Mar 20 '17 at 8:50
You're right. I was using
pgcli
which for some reason gives a different result.– Flimm
Mar 20 '17 at 16:04
pgcli
This work with database owned by group role:
SELECT
U.rolname
,D.datname
FROM
pg_authid AS U JOIN pg_database AS D ON (D.datdba = U.oid)
WHERE
D.datname = current_database()
The follwing query displays info for all tables in the public schema:
select t.table_name, t.table_type, c.relname, c.relowner, u.usename
from information_schema.tables t
join pg_catalog.pg_class c on (t.table_name = c.relname)
join pg_catalog.pg_user u on (c.relowner = u.usesysid)
where t.table_schema='public';
source :http://cully.biz/2013/12/11/postgresql-getting-the-owner-of-tables/
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This won't work if database is owned by a role (a group of users) and not a user.
– JC Boggio
Sep 25 '16 at 10:20