How to assign select permission for all tables? - sql

I want to assign select permission to a user, for all tables, and I need this permission work for every table which will be added to database.
I can assign select permission to my user (data_reader) for all tables which are currently in database, by executing my code result:
select 'grant select on object::'+s.name+'.'+t.name+' to data_reader'
from sys.schemas s
inner join sys.tables t on s.schema_id=t.schema_id
my problem is, if any table added to database I have to execute grant select on that table for data_reader, so it may be forgotten.
I searched a lot, but I couldn't find a way to grant select to all tables in all schemas to my user, forever.

If all your tables are in the dbo schema then this would be :
GRANT SELECT ON SCHEMA :: [dbo] TO specialrole
and try it :
GRANT { { schemaPrivileges | SELECT [ PRIVILEGES ] } ON { SCHEMA <schema_name> | ALL SCHEMAS IN DATABASE <db_name> }
| { schemaObjectPrivileges | SELECT [ PRIVILEGES ] } ON FUTURE <object_type>S IN SCHEMA <schema_name> }
TO [ ROLE ] <role_name> [ WITH GRANT OPTION ]

It is enough to give the rights db_datareader in the mapping to the desired database:
USE [DBName]
GO
ALTER ROLE [db_datareader] ADD MEMBER [user_name]
GO

I check this problem on SQL Server 2017
I create a login with Stack Name and map to Data Base User without any permission
Then i grant Select permission to user
grant select to Stack;

Related

Restrict user to certain tables in SQL

We have a azure based SQL Server instance that we are wanting to create a user who would only have SELECT access to two tables on a database that has many more tables. Is this possible? If so is there a guide I can follow to accomplish this?
Restrict user to certain tables in SQL
You can use the SQL GRANT statement to grant SQL SELECT, and other privileges on tables.
CREATE USER demouser
WITH PASSWORD = 'demopa#ssword1234';
GRANT SELECT ON dbo.student TO demouser
GRANT SELECT ON dbo.Airlines TO demouser
execute as user = 'demouser'
select * from student
select * from Airlines
REVERT;
Execution
Granted SELECT permission to user demouser on Airlines and student table.
demouser user has no access to mydata table

SYS account can not view all tables

As you can see in picture I am connecting to my database as SYS with the SYSDBA below
The filtered tables shown in the picture 2 seems are system tables, right, as all tables names are ended with $?
I am wondering why SYS does not have permission to view the tables we created e.g filetable (as shown in the picture above). Is it because this ID does not have permission to select all tables? If yes, how can I allow the ID have permission to view all tables, including filetable?
Does this SYS account have FULL privileges in the database or not? I tried to use the below command to grant privileges to user but that didn't work, getting the error shown:
SQL> grant SELECT on filetable to user
grant SELECT on filetable to user
*
ERROR at line 1:
ORA-00942: table or view does not exist
What I need to do is create a user and grant select permission on all tables to this user.
What I need to do ?
Simple run
select owner, table_name from dba_tables where table_name= 'FILETABLE';
or fallback to query below if it is not a table, but a view or a materialized view
select owner, object_name, object_type from dba_objects where object_name= 'FILETABLE';
than take what you see in the column owner and query with (substitue [owner] with the owner you see) - if the query above return no rows the object does not exists.
select * from [owner].FILETABLE

Redshift Table Invisible to Owner

I'm creating a table with a superuser (admin), and trying to GRANT ALL and change the owner of a table.
I'm running the following statements in Redshift, where (non-superuser) john is a member of the developers group (and only a member of the developers group):
GRANT ALL ON users.addresses TO GROUP developers;
ALTER TABLE users.addresses OWNER TO john;
I've confirmed that user john has select/insert/delete/update permissions on users.addresses, and is the owner of the table. I've also confirmed that user john has USAGE on the users schema.
However, when I log in as john, the table simply does not appear. When I try a SELECT * FROM users.addresses, Redshift says that the table does not exist.
What am I missing here? Is there an extra layer of permissions or security in Redshift that I'm not seeing? I've looked through the documentation, but haven't had much luck so far.
Because Redshift is saying that the table doesn't exist, that points towards the CREATE TABLE not being committed to the database when you're running it as a superuser account. If you disconnect as the superuser account and then reconnect and attempt to run the SELECT statement against the table, do you get the same error?
Try running an explicit COMMIT statement after you create the table, and then attempt querying it using the non-superuser account. If the client you are using is wrapping all query executions inside of a transaction block, it is possible that they aren't being committed prior to you connecting with the non-superuser account.
Also for reference, I ran the following queries with auto-commit enabled and was unable to replicate the issue you're describing:
-- Run as superuser account:
CREATE SCHEMA users;
CREATE TABLE users.addresses (
user_id VARCHAR(8) ,
user_address VARCHAR(512)
);
INSERT INTO users.addresses VALUES ('12345678', 'Address 1');
CREATE USER john WITH PASSWORD '********';
CREATE GROUP developers;
GRANT USAGE ON SCHEMA users TO GROUP developers;
ALTER GROUP developers ADD USER john;
GRANT ALL ON users.addresses TO GROUP developers;
ALTER TABLE users.addresses OWNER TO john;
-- Run as non-superuser account 'john':
SELECT *
FROM users.addresses;
-- Result Set:
-- user_id user_address
-- 12345678 Address 1

How Grant Select access to view in schema

Need to Grant an access to only the view of XYZ schema, View internally call the Table from dbo Schema. need to restrict access to dbo table.
as i am only giving access to XYZ schema when i try to slect XYZ.VIEW. it gives and error:
Msg 229, Level 14, State 5, Line 2
The SELECT permission was denied on the object 'Table1', database 'servername', schema 'dbo'.
Query to Grant access to user
GRANT SELECT ON XYZ.VIEW TO user1
XYZ.VIEW Definition is below
SELECT NAME, Adress
FROM dbo.Table1
If you don't want the user to access the table and all of its contents and only access what you have in view, then create a Materialized view. This will isolate the user to what you give them and will remove the need to give them access to the table.

Oracle SQL creating user and granting permissions

I'm new to SQL and programming so I apologize if this isn't clearly worded.
I have the following code to create a user and role. I want user1 to be able to select and view table1.
When I run this code user1 isn't able to select table1 and I get the error that the table does not exist.
When prompted with the Database Entry screen I input user and password.
When prompted with the statement "Enter TNS_ADMIN entry or blank for Current Worksheet Database:" I don't input anything.
USER1 is able to connect, but not view the table. Under "other users" in the connection I see USER1 but none of the tables I've established are listed.
Thanks!
CREATE USER user1
IDENTIFIED BY user1
DEFAULT TABLESPACE users
TEMPORARY TABLESPACE temp
ACCOUNT UNLOCK;
CREATE ROLE ReadOnly;
GRANT SELECT ON table1 to ReadOnly;
--GRANT SELECT ON table2 to ReadOnly;
--GRANT SELECT ON table3 to ReadOnly;
GRANT CONNECT to user1;
GRANT ReadOnly to user1;
grant create session to user1;
connect user1
show user
select * from table1
Assuming that table1 is not owned by user1, you have three options
You can use the full name of the table in your query
SELECT * FROM <<schema name>>.table1
You can create a synonym for table1. A private synonym in the user1 schema would generally be preferred but a public synonym would also work. As user1
CREATE SYNONYM table1 FOR <<schema name>>.table1
SELECT * FROM table1;
Or you can change the default schema (for name resolution purposes, this has no impact on privileges)
ALTER SESSION SET current_schema=<<schema that owns table1>>
SELECT * FROM table1;