0

I am new in writing queries. I need a list of views that every user can write queries to.

SELECT *
FROM Information_Schema.Views;

I tried that, but I guess it is a list of all views, but I need only those views that all users could write queries to.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228

4 Answers4

1

The information about views is stored in information_schema.tables:

SELECT *
FROM Information_Schema.Tables
WHERE table_type = 'VIEW';
Kombajn zbożowy
  • 8,755
  • 3
  • 28
  • 60
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • but is it all views or only those that all users can write queries to them? – user4039853 Sep 14 '14 at 14:44
  • @user4039853 . . . The information about views is stored in `information_schema.tables`. I'm not sure what your question is, but system views are in there too. – Gordon Linoff Sep 14 '14 at 14:56
  • I need only those views that all users has access to them – user4039853 Sep 14 '14 at 15:00
  • The information schema consists of views exclusively, based on tables in the system catalogs. They obviously do not "store" information. More importantly, the presented query is inferior to what the question already presented and just as wrong - without crucial additional information (even after @Kombajn's edit). I added another answer. – Erwin Brandstetter Sep 15 '14 at 04:13
0

This will list each each users visible views (without the superusers.)

SELECT
    usename, schemaname||'.'|| viewname as view
FROM
    pg_views
    ,pg_user
WHERE
    has_table_privilege (
        pg_user.usename,
        schemaname||'.'|| viewname,
        'select'
     )
AND
    schemaname NOT IN (
        'pg_catalog',
        'information_schema'
     )
AND usesuper=false

As far as listing a view that is visible to all users ... Postgres has fine grained permissions so there is no easy flag to search for. You would have to match that each view would produce a row of every user. But you could create a group for your users and then query for that.

user17130
  • 241
  • 1
  • 6
0

If you are going to use the information schema, you need to be aware how it works. Per documentation:

The view views contains all views defined in the current database. Only those views are shown that the current user has access to (by way of being the owner or having some privilege).

Bold emphasis mine. This also provides a handy way of solving your problem. Run your query with a plain, new, non-superuser role that that wasn't granted membership in any other roles nor any direct privileges on any views (yet).
If you want to exclude system views add the WHERE clause:

SELECT *
FROM   information_schema.views
WHERE  table_schema NOT LIKE ALL ('{pg_%,information_schema}'::text[]);

You get only those views (and all of them), that public can access.

SQL Fiddle.

(Yes, use the query on information_schema.views you already had, no point in using information_schema.tables, like has been suggested.)

For more specific needs, I suggest you use the system catalogs instead. Actual privileges are stored in the system table pg_class in the column relacl. And the view pg_views lists all views, not just the ones the current role has privileges for.

Resolving actual privileges is not trivial. Use the dedicated "Access Privilege Inquiry Functions" like has_table_privilege() that @user17130 already suggested. Related answer:

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • what about this > `select * from INFORMATION_SCHEMA.views WHERE table_schema = ANY (current_schemas(false))` – Vivek S. Sep 15 '14 at 04:55
0
select * from pg_catalog.pg_views
where schemaname NOT IN ('pg_catalog', 'information_schema')
order by schemaname, viewname;
  • will show you the viewname and its owner
Vivek S.
  • 19,945
  • 7
  • 68
  • 85