Redshift view definition permissions issue - amazon-redshift

Running this query, even as postgres login, displays view_definition only for the views that are created by the current login.
select table_schema as schema_name, table_name as view_name, view_definition
from information_schema.views
where table_schema not in ('information_schema', 'pg_catalog')
order by schema_name, view_name;
Is there some way to get view definition for all views no matter who created them?
If not i plan to just make a job in my etl scheduler to reassign all views to owner postgres.

Information_schema.views returns NULL for view_definition if the current user (even admin) is not the owner of the view [1]. This is how it is designed in Postgres and since Redshift is based on Postgres 8.0.2, I assume it has a similar behavior.
The v_generate_view_ddl [2] admin view in AWS's GitHub repo could help in getting all view definitions.
References:
[1] Postgres Documentation - https://www.postgresql.org/docs/8.0/infoschema-views.html
[2] Redshift Admin View - https://github.com/awslabs/amazon-redshift-utils/blob/master/src/AdminViews/v_generate_view_ddl.sql

You could just use "show view" command and it will show the view definition irrespective of who created it in redshift.
show view <nameoftheview>

Related

SQL developer version 21.2 view definition not displaying.view sql don't display definition

I am using sql developer version 21.( Recently installed) I can't access the view sqls/ definition from the view tab. I can accessor see the view text from " details" tab but not from the "Sql" tab.
I don't have admin privilege.
The same user can view view sqls from sqldeveloper version 18...
In older versions of SQL Developer we had a 'try to generate DDL' method for when DBMS_METADATA.GET_DLL() wasn't available.
This wasn't a maintainable position. The 'internal generator' has multiple issues, and we decided to deprecate it.
In order to see the DDL for an object, you need for the DBMS_METADATA package to be available to your user, for said object.
What SQL Developer runs to get you the DDL for a VIEW, is approx:
SELECT
dbms_metadata.get_ddl(
'VIEW',
:name,
:owner
)
FROM
dual
UNION ALL
SELECT
dbms_metadata.get_ddl(
'TRIGGER',
trigger_name,
owner
)
FROM
dba_triggers
WHERE
table_owner = :owner
AND table_name = :name
UNION ALL
SELECT
dbms_metadata.get_dependent_ddl(
'COMMENT',
table_name,
owner
)
FROM
(
SELECT
table_name,
owner
FROM
dba_col_comments
WHERE
owner = :owner
AND table_name = :name
AND comments IS NOT NULL
UNION
SELECT
table_name,
owner
FROM
sys.dba_tab_comments
WHERE
owner = :owner
AND table_name = :name
AND comments IS NOT NULL
)
In a development environment, a developer should have full access to their application, and I would extend that to the data dictionary. It's another reason I advocate developers have their own private database (Docker/VirtualBox/Cloud/whatever).
If that fails, consult your data model.
If you don't have a data model, that's another problem.
If that fails, you do have workaround of checking the Details panel for a view to get the underlying SQL.
Just FYI, I searched for an answer to this problem and found no actual solutions.
thatjeffsmith was correct that earlier versions of SQLDeveloper do not have this issue or requirement of higher privs to view the SQL tab. However, the link he provided was version 20.4 and it sill did not display the SQL tab correctly. I reverted back to 3.1.07 (which I happened to be using prior to upgrading my laptop) and using the same login to the same instance it does display the SQL for views, full definition, without issue. This is against a 12c Oracle database.

How can I see the sql statement of a view (db resides on AWS)

I've just installed the vscode extension (Oracle Developer Tools for VS Code (SQL and PLSQL)
) and successfully connected the db.
The db resides on AWS.
I can connect the db and just wanted to test it by opening an existing view.
But, it just lets me "describe" the view. So I can see the columns but I need to edit the query statement.
What's missing? Or is the problem the AWS part?
I usually use SQL Developer but I'm furthermore interested in backing up the work via git commits. And I like the way "git graph" extensions presents the changes.
DDL view_name
Or
SELECT
text_vc
FROM
dba_views
WHERE
owner = :schema AND
view_name = :view_name;
With help from someone of the Oracle community I managed to get it working.
Basic query is:
select
dbms_metadata.get_ddl('VIEW', 'VIEW_NAME', 'VIEW_OWNER')
from
dual;
So, in my case it is:
select
dbms_metadata.get_ddl('VIEW', 'ALL_DATA_WAREHOUSE_BOSTON', 'WHB')
from
dual;
Owner is the name you fill in when connection to the database, which is the key/value pair (username/password).
If you are not sure who the owner of the view is, check it with this query:
select owner from ALL_VIEWS where VIEW_NAME ='ALL_DATA_WAREHOUSE_BOSTON';

check if materialized view is populated

Using postgres 9.5.2 (python client)
Is there any way to check if a materialized view is populated using a query? One that does not raise a warning the view is not populated?
The quick answer:
SELECT relispopulated FROM pg_class WHERE relname = '<the table name>';
You can find more details about the pg_class table in the documentation. According to that, the field relispopulated should be true for everything but some materialized views, and I infer from that and from the name of the field, that it will give you what you want.

Check Postgres access for a user

I have looked into the documentation for GRANT Found here and I was trying to see if there is a built-in function that can let me look at what level of accessibility I have in databases. Of course there is:
\dp and \dp mytablename
But this does not show what my account has access to. I would like to see ALL the tables I have access to. Can anyone tell me if there is a command that can check my level of access in Postgres (whether I have SELECT, INSERT, DELETE, UPDATE privileges)? And if so, what would that command be?
You could query the table_privileges table in the information schema:
SELECT table_catalog, table_schema, table_name, privilege_type
FROM information_schema.table_privileges
WHERE grantee = 'MY_USER'
For all users on a specific database, do the following:
# psql
\c your_database
select grantee, table_catalog, privilege_type, table_schema, table_name from information_schema.table_privileges order by grantee, table_schema, table_name;
Use this to list Grantee too and remove (PG_monitor and Public) for Postgres PaaS Azure.
SELECT grantee,table_catalog, table_schema, table_name, privilege_type
FROM information_schema.table_privileges
WHERE grantee not in ('pg_monitor','PUBLIC');

Show table structure and list of tables in PostgreSQL [duplicate]

This question already has answers here:
PostgreSQL "DESCRIBE TABLE"
(24 answers)
Closed 6 years ago.
I had employed MySQL for a couple of former projects. But now have
decided to switch to PostgreSQL. Not that version 8 also works on that,
ahem other OS which I'm stuck with at work.
But alas, two of the most useful commands appear to be missing:
SHOW TABLES
DESCRIBE table
Inasmuch as my prototyping DB is on my NetBSD server at home while my
data waiting to be 'based is at work, such that I have to connect via
Perl/DBI and XML-RPC (not psql, alas). The IT dept here just says, "Use
MS-Access", so no help there.
While I'm in the initial stage I need an informative way to blunder
around and see what's what as I try different ways to build this thing.
For that I had always relied on the two above from MySQL.
I can't believe there is no way for PostgreSQL to tell me what the
current DB's table structure is via simple SQL queries executed remotely.
Surely there must be. But I can't seem to find out from the couple of
books I have. All I dug up was some ultra-lame hack to get column names
for an already known table name by doing a "WHERE 1 != 1" or some such
so that no actual rows could be returned. Not very informative, that.
Surely I've missed the point, somewhere.
So enlighten me, please. What, pray tell, are the PostgreSQL-ish SQL
queries one uses so as to explore a given DB's table structure? What is
the PostgreSQL translation for "SHOW TABLES" and "DESCRIBE table"?
SHOW TABLES and DESCRIBE TABLE are MySQL-specific admin commands, and nothing to do with standard SQL.
You want the:
\d
and
\d+ tablename
commands from psql.
These are implemented client-side. I find this odd myself, and would love to move them server-side as built-in SQL commands one day.
Other clients provide other ways to browse the structure - for example, PgAdmin-III.
If you want a portable way to get table structure in code, you should use the information_schema views, which are SQL-standard. See information_schema. They're available in MySQL, PostgreSQL, Ms-SQL, and most other DBs. The downside is that they're fiddlier to use, so they aren't convenient for quick access when you're just browsing a DB structure.
As per the Documentation
SELECT
table_schema || '.' || table_name as show_tables
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema NOT IN ('pg_catalog', 'information_schema');
for more convenience make it as a function
create or replace function show_tables() returns SETOF text as $$
SELECT
table_schema || '.' || table_name as show_tables
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema NOT IN ('pg_catalog', 'information_schema');
$$
language sql;
So we can get the tables using
select show_tables()
For the table description
select column_name, data_type, character_maximum_length
from INFORMATION_SCHEMA.COLUMNS where table_name ='table_name';
as a Function
create or replace function describe_table(tbl_name text) returns table(column_name
varchar, data_type varchar,character_maximum_length int) as $$
select column_name, data_type, character_maximum_length
from INFORMATION_SCHEMA.COLUMNS where table_name = $1;
$$
language 'sql';
select * from describe_table('a_table_name');