How to change hive table owner - hive

I have a hive table 'sample' created with owner as 'X'
hive> show table extended like sample;
--shows owner as 'X'
Is there a way I can change the owner to some other 'Y', without recreating the table (don't want to lose the data)
A known option is to update the owner directly in the postgres hive metastore table.
hive=# update "TBLS" set "OWNER" = 'Y' where "OWNER" = 'X' and "TBL_NAME" = 'sample';
Is this safe?

As I know there is no other way to do it besides direct change in metastore DB. I needed to do this couple of times - there were no any issues.

I just experienced this issue and can share my resolution notes, to add color to what a "Direct Change in The Metastore DB" for me. In our configuration, we're using Presto that connects to Hive. Tables will be created in hive with whatever user that Presto connects as (with the --user flag on the Presto CLI).
We were getting an error message such as:
Access Denied: Cannot drop table SCHEMA.TABLE_NAME: Owner of the table is
different from session user
I can see the users of the table by executing the following query on the Hive Metastore:
select t.OWNER, p.PRINCIPAL_NAME, count(1)
from TBLS t
join TBL_PRIVS p on p.TBL_ID=t.TBL_ID
group by t.OWNER, p.PRINCIPAL_NAME;
And then, I can update the tables as needed by executing:
update TBLS set OWNER='NEW_OWNER' where OWNER='OLD_OWNER';
update TBL_PRIVS set PRINCIPAL_NAME='NEW_OWNER' where PRINCIPAL_NAME='OLD_OWNER';
NOTE: You should run this in a transaction and make sure your Metastore is backed up first.

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>'));

Can't do a CTAS when I use dbname in CTAS

A piculiarity I noticed.
When I try
create table dbname.table_name as select
I get Error creating temporary folder on: hdfs://nameservice1/apps/hive/warehouse. Error encountered near token 'TOK_TMP_FILE'
But If I first do
use dbname;
and then
create table table_name as select
It works. Why is that?
To create table in any database user need to have write permission on current database and database in which table is being created.
I.e. while running create table dbname.table_name as select statement , you need to have write permission on current database as well.
This is known issue reported in jira HIVE-11427.

DBA readonly account

I had a schema in one oracle DB as ui_prod. I asked my DBA team guys to create exactly same schema like ui_prod but as read only and name it ui_prod_readonly. Usually I will use Oracle SQL developer to connect a DB and query directly with table name like below.
--Connect to ui_prod
select * from table
but why I requested to put owner name infront when query for readonly schema they created for me, as without putting it, I get error table not exist.
--Connect to ui_prod_readonly
select * from ui_prod.table
I have project files which hardcode the sql query with only table names and adding owner name in front will cause many changes and effort. Can anyone explain me on this? or provide me any document/link to read. Thanks
You should look into synonyms, apparently the user you are connecting to the database as is not the owner of the objects. So to view the object you have to prepend the names with the schema name (the owner of the object themselves).
http://www.techonthenet.com/oracle/synonyms.php
CREATE OR REPLACE SYNONYM ui_prod_readonly.synonym_name
FOR ui_prod.object_name
It seems to me that your dbas have not created another set of tables but just granted the existing tables to the user ui_prod_readonly.
When you log in to Oracle, the current schema is the name of the user you used to log in. So if you log in with ui_prod_readonly Oracle checks that schema for the table if you do not qualify it with the owner (=schema).
If you want to change the current schema so that you don't need to fully qualify the tables, you can do that with ALTER SESSION
alter session set current_schema = ui_prod;
Once you have done that, you don't need to fully qualify the table with the owner (=schema).
if you need a user to read the data only
its simple to create new user and grant it only select privilege
you can create user and grant select privilege using
CREATE USER [user] IDENTIFIED BY [your_password];
grant select on table to [user]

SELECT data from another schema in oracle

I want to execute a query that selects data from a different schema than the one specified in the DB connection (same Oracle server, same database, different schema)
I have an python app talking to an Oracle server. It opens a connection to database (server/schema) A, and executes select queries to tables inside that database.
I've tried the following :
select ....
from pct.pi_int, pct.pi_ma, pct.pi_es
where ...
But I get:
ORA-00942: table or view does not exist
I've also tried surrounding the schema name with brackets:
from [PCT].pi_int, [PCT].pi_ma, [PCAT].pi_es
I get:
ORA-00903: invalid table name
The queries are executed using the cx_Oracle python module from inside a Django app.
Can this be done or should I make a new db connection?
Does the user that you are using to connect to the database (user A in this example) have SELECT access on the objects in the PCT schema? Assuming that A does not have this access, you would get the "table or view does not exist" error.
Most likely, you need your DBA to grant user A access to whatever tables in the PCT schema that you need. Something like
GRANT SELECT ON pct.pi_int
TO a;
Once that is done, you should be able to refer to the objects in the PCT schema using the syntax pct.pi_int as you demonstrated initially in your question. The bracket syntax approach will not work.
In addition to grants, you can try creating synonyms. It will avoid the need for specifying the table owner schema every time.
From the connecting schema:
CREATE SYNONYM pi_int FOR pct.pi_int;
Then you can query pi_int as:
SELECT * FROM pi_int;
Depending on the schema/account you are using to connect to the database, I would suspect you are missing a grant to the account you are using to connect to the database.
Connect as PCT account in the database, then grant the account you are using select access for the table.
grant select on pi_int to Account_used_to_connect

show all tables in DB2 using the LIST command

This is embarrassing, but I can't seem to find a way to list the names of the tables in our DB2 database. Here is what I tried:
root#VO11555:~# su - db2inst1
root#VO11555:~# . ~db2inst1/sqllib/db2profile
root#VO11555:~# LIST ACTIVE DATABASES
We receive this error: SQL1092N "ROOT" does not have the authority to perform the requested command or operation.
The DB2 version number follows.
root#VO11555:~# db2level
DB21085I Instance "db2inst1" uses "64" bits and DB2 code release "SQL09071"
with level identifier "08020107".
Informational tokens are "DB2 v9.7.0.1", "s091114", "IP23034", and Fix Pack
"1".
Product is installed at "/opt/db2V9.7".
To get a list of tables for the current database in DB2 -->
Connect to the database:
db2 connect to DATABASENAME user USER using PASSWORD
Run this query:
db2 LIST TABLES
This is the equivalent of SHOW TABLES in MySQL.
You may need to execute 'set schema myschema' to the correct schema before you run the list tables command. By default upon login your schema is the same as your username - which often won't contain any tables. You can use 'values current schema' to check what schema you're currently set to.
Connect to the database:
db2 connect to <database-name>
List all tables:
db2 list tables for all
To list all tables in selected schema, use:
db2 list tables for schema <schema-name>
To describe a table, type:
db2 describe table <table-schema.table-name>
credit http://onewebsql.com/blog/list-all-tables
select * from syscat.tables where type = 'T'
you may want to restrict the query to your tabschema
I'm using db2 7.1 and SQuirrel. This is the only query that worked for me.
select * from SYSIBM.tables where table_schema = 'my_schema' and table_type = 'BASE TABLE';
Run this command line on your preferred shell session:
db2 "select tabname from syscat.tables where owner = 'DB2INST1'"
Maybe you'd like to modify the owner name, and need to check the list of current owners?
db2 "select distinct owner from syscat.tables"
have you installed a user db2inst2, i think, i remember, that db2inst1 is very administrative