create schema bla;
-- then create table table_name into this schema
Then I want change default schema for user (user is postgres)
I do: ALTER ROLE postgres SET search_path TO bla; (Query returned successfully with no result).
When I try SELECT * FROM table_name gives error relation "table_name" does not exist
Though SELECT * FROM bla.table_name works fine.
What is wrong in my attempt to change default schema for user?
I think you need to relogin for that. With ALTER USER ... SET you change
Session defaults for run-time configuration variables
Also from ALTER ROLE SET manual:
Role-specific variable settings take effect only at login;
But don't apply changes to current session. If you want immediate change use:
SET search_path TO bla;
It will change path on session level
In PostGres the exact command would be
ALTER USER 'your-user' set SEARCH_PATH = 'schema_name';
Related
I am using AdventureWorksLT and each of the tables has the SalesLT Schema;
each time I have to do a query I need to make a reference to the SalesLT Schema:
SELECT * FROM SalesLT.Address
This makes querying very extensive; is there a way to refer to the SalesLT Schema, without having to write it in every query e.g.
SELECT * FROM Address
If you do not want to give the schema name you can set the default schema for a user -
To Set the default Schema
ALTER USER [yourUser] WITH DEFAULT_SCHEMA = [YourSchema];
To See the Schema -
SELECT SCHEMA_NAME()
Use ALTER SCHEMA to define the default schema. This change is permanent until you run this command again:
ALTER USER <user> WITH DEFAULT_SCHEMA = <my-schema>
As far as I know, there's no way of altering the default schema just for the session. It's a permanent change.
I am giving the permissions as per given query.
Now I want to remove test1 DB from user test.
ALTER AUTHORIZATION ON DATABASE::test TO test;
ALTER AUTHORIZATION ON DATABASE::test1 TO test;
Assign ownership back to dbo which is default owner of the database.
ALTER AUTHORIZATION ON DATABASE::test1 TO dbo
OR
You can give the ownership to whom you want to assign instead of giving it to dbo.
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]
The scenario
CREATE SCHEMA testschema;
CREATE ROLE testrole LOGIN;
GRANT ALL ON SCHEMA testschema TO testrole;
ALTER ROLE testrole SET search_path = testschema;
Now if I initiate the connection (log in) as testrole then:
SHOW search_path;
Gives the desired result:
search_path
-------------
testschema
(1 row)
However, if I initiate connection (log in) as a superuser and do:
SET SESSION AUTHORIZATION testrole;
SHOW search_path;
Results in:
search_path
----------------
"$user",public
(1 row)
(or whatever the search path of the superuser is)
My question is, why does SET SESSION AUTHORIZATION not affect the current search_path?
Is it a bug, by design or am I simply doinitwrong?
From the little I've found, the workaround of SET SEARCH path = schemaname after SET SESSION... seems to be the only solution, but that kind of defeats the purpose of having persistent search paths assigned to roles.
This is by design. I quote the manual on ALTER ROLE
This only happens at login time; executing SET ROLE or SET SESSION
AUTHORIZATION does not cause new configuration values to be set.
Question: when I create a table (T_TableName) using SQL Server Management-Studio, it always creates the table as
Domain\UserName.T_TableName
instead of
dbo.T_TableName
What's wrong ?
If you don't specify a schema explicitly on your table name to be created, it will be created in the user's current default schema.
I bet the user you're using has its own personal schema set as its default schema - that's why your tables get created in his own personal schema.
You can check what database users you have and what their default schema is by inspecting sys.database_principals (SQL Server 2005 and up):
SELECT name, type_desc, default_schema_name
FROM sys.database_principals
To solve this:
specify the schema you want to use explicitly (best practice anyway!)
CREATE TABLE dbo.T_TableName
change the user's default schema to dbo
ALTER USER [Domain\YourUser] WITH DEFAULT_SCHEMA = dbo
But as a general rule of thumb, I recommend always using the "dbo." prefix explicitly, if you want to have all your database objects in the dbo schema. Helps with performance, too (ever so slightly) since SQL Server won't have to go hunting in different schemas, if you explicitly tell it where your db objects live.
You need to either create your table as "dbo.Whatever", OR you need to change your default schema (or have your SA do it for you) by issuing a command like:
ALTER USER [DOMAINNAME\UserName] WITH DEFAULT_SCHEMA = dbo;
Call it dbo.T_TableName in SSMS. If you have the correct permissions, it will work.
Are you assigned as db_owner for the database you created the table in? If not, this could be the issue. Try adding your user mapping permissions to the database as such.
USE [yourDatabase]
GO
EXEC sp_addrolemember N'db_owner', N'DOMAIN\UserOrGroup'
GO