PostgreSQL Views Query - sql

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.

The information about views is stored in information_schema.tables:
SELECT *
FROM Information_Schema.Tables
WHERE table_type = 'VIEW';

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.

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

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:
How to check if a table exists in a given schema

Related

How to get count of tables present in a particular schema in snowflake?

I am trying to find the count of tables present in CDC schema, I tried with information_schema but the information_schema is empty for me.
The following SQL would give you the required results:
use schema <schemaname>;
show tables;
The result shows the number of tables in the schema.
Note: The information schema in a particular database only displays the objects to which your current role in the session has access.
Please make sure that the role you are using has required access control privileges to view the database objects. If not, if you have access to high privilege roles like SYSADMIN/ACCOUNTADMIN you could use them when viewing metadata information in Snowflake.
You can try querying the INFORMATION_SCHEMA.TABLES View with a query :
select count(distinct TABLE_NAME)
from <database_name>.INFORMATION_SCHEMA.TABLES
where TABLE_CATALOG = <database_name>
and TABLE_SCHEMA = <schema_name>;
Alternatively, you can also use the SHOW TABLES command as follows to get the count.
show tables in schema <DATABASE_NAME>.<SCHEMA_NAME>;
select count(*) from table(result_scan('<last_query_id>'));

How to list all selectable tables with current user in SAP HANA DB

I want to list all tables with current user using sql.
I know there are some way to get privileges, like EFFECTIVE_PRIVILEGES, but it only shows privileges which is granted by GRANT SELECT ~ on [TABLE] to [USERS].
You can query the schema information to get this - there's no SHOW TABLES or equivalent available.
Try:
SELECT TABLE_NAME FROM "SYS"."TABLES" WHERE SCHEMA_NAME='<database_name>'
which works for me.
You can also get Column Names in thsi way from the SYS.COLUMNS view

How can I list all tables existent in a Database Link (Oracle)?

Basically I have a Database link (Oracle) called mylink.domain, in this link we can access foundation information like name of them members and other general information.
I would like to list all table's name in this link but i don't know how to do that.
Thanks in advance
You can access the all_tables view through the dblink:
select owner, table_name
from all_tables#dblink
order by owner, table_name;
Selecting the content of ALL_TABLES dictionary view will list you all tables your user has access to. Generally, it is not always possible to get a list of tables you dont have permissions for - they just do not show up. If your user has the SELECT ANY DICTIONARY priviledge, you can select the content of DBA_TABLES, which will always list all tables existing in the database.
select table_name from all_tables#dblinkname;
This shows all tables your linked user has access to.
Where I got the answer from

Getting all tables from an oracle database

I'm trying to retrieve a list of tables from an Oracle connection. I'm not very familiar with Oracle terminology and thus, having hard time finding the information I need.
Right now I can use Microsoft Access to connect via ODBC and it pops up with a "Link Tables" dialog that lists all tables, not just the ones I "own". None of the queries I've tried so far, give me this data.
I'm trying "SELECT * FROM all_tables" but that doesn't show me the right data.
ALL_TABLES will show you all the tables that you have access to SELECT from. DBA_TABLES will show you all the tables that exist in the database though you'll need an additional privilege grant to be able to query the DBA* data dictionary objects.
Try select * from all_tables, that should do what you want.
It can be.. (If user has dba role)
select * from dba_tables
SELECT owner, table_name
FROM all_tables
You can also try
SELECT * FROM USER_TABLES
It will return list of tables owned by your user.
SELECT * FROM TAB; that will show you all the table and views

how to select a Oracle schema-specific view from another schema

Suppose I'm logged in as USERA, I want to access all the user_* views of the USERB schema, such as user_tables, user_tab_columns. How can I do this? Thanks
All the USER_* tables have analogues with the ALL_* and DBA_* prefix. USER_TABLES has information about all the tables you own. ALL_TABLES has information about all the tables you have access to. DBA_TABLES has information about all the tables in your database.
If you want to see information about UserB's tables
SELECT *
FROM all_tables
WHERE owner = 'USERB';
or
SELECT *
FROM dba_tables
WHERE owner = 'USERB';
The former will work if you have SELECT access on User B's tables. The latter will work if your DBA has given you access to the DBA_TABLES view. That is normally done by granting the SELECT ANY DICTIONARY privilege (or the SELECT_CATALOG_ROLE in prior version) though the DBA can grant access to individual DBA_* views.
USER_% views give what you own, that is what's inside your schema.
ALL_% views give what you have access to.
So what you really should use is ALL_TABLES/etc, and grant appropriate access to USERB objects.
Assuming you have permissions, you could try:
ALTER SESSION SET CURRENT_SCHEMA=USERB;