0

Im trying to now only display the user, schema & tables but also show the database the schema & tables belong to. Any suggestions on what I should add to this query?

SELECT
    u.usename as user, t.schemaname as schema, t.tablename as table,
    has_table_privilege(u.usename,t.schemaname||'.'||t.tablename,'select') AS "Select",
    has_table_privilege(u.usename,t.schemaname||'.'||t.tablename,'insert') AS "Insert",
    has_table_privilege(u.usename,t.schemaname||'.'||t.tablename,'update') AS "Update",
    has_table_privilege(u.usename,t.schemaname||'.'||t.tablename,'delete') AS "Delete",
    has_table_privilege(u.usename,t.schemaname||'.'||t.tablename,'references') AS "Reference"
FROM pg_user u
CROSS JOIN pg_tables t
WHERE t.schemaname != 'information_schema' and t.schemaname != 'pg_internal' and t.schemaname != 'pg_catalog' and t.tablename not like '% %'
ORDER BY u.usename, t.schemaname, t.tablename;
3
  • Please add a tag for the specific type of SQL you're working with (mysql, oracle, ms sql, teradata, etc) Commented May 27, 2020 at 20:58
  • @devlincarnate I added the tag for postgresql Commented May 27, 2020 at 21:04
  • You can simply add a result column current_database(). Commented May 28, 2020 at 6:13

1 Answer 1

2

I suggest looking into information_schema.*. These are sql standards and information_schema.tables should have what you want.

You can replace the use of pg_tables as such:

select
    u.usename                                                                           as user,
    t.table_catalog                                                                     as database,
    t.table_schema                                                                      as schema,
    t.table_name                                                                        as table,
    has_table_privilege(u.usename, t.table_schema || '.' || t.table_name, 'select')     as "Select",
    has_table_privilege(u.usename, t.table_schema || '.' || t.table_name, 'insert')     as "Insert",
    has_table_privilege(u.usename, t.table_schema || '.' || t.table_name, 'update')     as "Update",
    has_table_privilege(u.usename, t.table_schema || '.' || t.table_name, 'delete')     as "Delete",
    has_table_privilege(u.usename, t.table_schema || '.' || t.table_name, 'references') as "Reference"
from
    pg_user                              u
    cross join information_schema.tables t
where
    t.table_schema != 'information_schema' and t.table_schema != 'pg_internal' and t.table_schema != 'pg_catalog'
    and t.table_schema not like '% %'
order by
    u.usename, t.table_schema, t.table_name;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.