How to create and use Synonyms on PostgreSQL as in Oracle. Do I need to create some DB link or any thing else. I could not find any good official doc on this topic.
Edit 1
Actually as of now i have an application which has two separate modules which connects with two different oracle databases; One modules need to access tables of other so for which we use synonyms over db link in oracle. Now we are migrating application to postgresql, so we need synonyms.
Edit 2
When i say two different oracle databases it means it can be two different oracle instances or two schemas of same db, it is configurable in application and application must support both modes.
PostgreSQL version: 9.6.3
Approach 1:-
Finally i got it working using foreign data wrapper postgres_fdw as below
I have two databases named dba and dbb. dbb has a table users and i need to access it in dba
CREATE SERVER myserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost', dbname 'dbb', port '5432');
CREATE USER MAPPING FOR postgres
SERVER myserver
OPTIONS (user 'user', password 'password');
CREATE FOREIGN TABLE users (
username char(1))
SERVER myserver
OPTIONS (schema_name 'public', table_name 'users');
CREATE FOREIGN TABLE users (users char(1));
Now i can execute all select/update queries in dba.
Approach 2:-
Can be achieved by creating two schemas in same db, below are the steps:
create two schemas ex app_schema, common_schema.
Grant access:
GRANT CREATE,USAGE ON SCHEMA app_schema TO myuser;
GRANT CREATE,USAGE ON SCHEMA common_schema TO myuser;
Now set search path of user as below
alter user myuser set search_path to app_schema,common_schema;
Now tables in common_schema will be visible to myuser. For example let say we have a table user in common_schema and table app in app_schema then below queries will be running easily:
select * from user;
select * from app;
This is similar to synonyms in oracle.
Note- Above queries will work PostgreSQL 9.5.3+
I think you don't need synonyms in Postgres the way you need them in Oracle because unlike Oracle there is a clear distinction between a user and a schema in Postgres. It's not a 1:1 relationship and multiple users can easily use multiple schemas without the need to fully qualify the objects by exploiting Postgres' "search path" feature - mydb.public.mytable.
If the tables are supposed to be in a different database in PostgreSQL as well, you'd create a foreign table using a foreign data wrapper.
If you used the Oracle synonym just to avoid having to write atable#dblink, you don't have to do anything in PostgreSQL, because foreign tables look and feel just like local tables in PostgreSQL.
If you use the synonym for some other purposes, you can either set search_path to include the schema where the target table is, or you can create a simple view that just selects everything from the target table.
Related
I have situation where I have to give schema wise access to users where the user should not even be able to see other schema names as well as its contents like tables. I have tried below queries :
create user potter with password 'potter';
create schema if not exists potter AUTHORIZATION potter;
set search_path to 'potter';
create table pottertable(id numeric);
grant SELECT, UPDATE, INSERT ON potter.pottertable to potter;
In this case when connected to the server (PostgreSQL 9.6 localhost) as new user using pgAdmin 4, the user is not able to SELECT other schema data but able to see other schema names as well as the tables inside it, which is what I am looking to prevent. Any help is appreciated.
I am trying to complete a tutorial on a simple javaEE project using wildfly. The first step is creating two tables in my database. As it says I should create my tables like this: "CREATE TABLE wildfly.name...." but it gives me an error saying thet wildfly is unknown.
Link to the tutorial: click here
My question is why should i put "wildfly." before the table name and how can I solve this error?
Thank you for your help!
Note: I am using oracle database instead of mysql
It's a misleading MySQL tutorial example because in Oracle syntax "wildfly." is a user(schema) in the Oracle database.
Schema/user in Oracle is a namespace for tables and other objects. So, when you issue such a statement - you're telling oracle to create table in namespace WILDFLY. If you don't have such user in your database or you don't have rights to access such user/schema - you can't create tables there.
You should create such user in Oracle database (or alter your statement to another user/schema name that you actually have in your database) and put your tables there.
For example these statements are correct because I created WILDFLY user before putting tables to it:
CONNECT SYS/****#ORCL AS SYSDBA
CREATE USER WILDFLY IDENTIFIED BY WILDFLYPASSWORD;
GRANT UNLIMITED TABLESPACE TO WILDFLY;
CREATE TABLE WILDFLY.MYTABLE...
Is it possible to alter schema of a database I am not connected to? More specifically I need to change an owner of a schema (but it doesn't matter for the questions' sake).
As documentation says schemata can be altered using a clause like:
ALTER SCHEMA name OWNER TO { new_owner | CURRENT_USER | SESSION_USER }
and it sure works, but only on a database I am currently connected in.
Sure I can reconnect to the other database and do it manually, but I am interested whether it is possible to do it from a connection to another (typically postgres) database. It would be quite helpful for automation processes.
I have tried something like:
ALTER DATABASE ALTER SCHEMA name OWNER TO ...
ALTER SCHEMA "db_name".name OWNER TO ...
But without success - so I am interested whether it is possible at all.
I tried to search for this information using one popular search engine and StackOverflow search feature as well. Unsuccessfully - hence the question.
As #a_horse_with_no_name and #JacobH pointed out in comments it is not possible to alter schema of a database you are not currently connected to.
So I ended up using a command like this in order to achieve the schema alteration:
psql $PG_DATABASE -c "ALTER SCHEMA \"<schema-name>\" OWNER TO $PG_USER";
I use Schema in my database only for grouping tables, views, stored procedure, functions and other object by subject, and I dont realy know where schema must be used, and why schema tab is below security tab in SSMS.
EDIT :
Schemata are a way to logically group objects so that consistent permissions can be applied to all of them through the schema rather than individually. Consider:
create schema [foo] authorization [dbo]
grant select on schema::[foo] to [user1]
create table [foo].[table_1] (...)
create table [foo].[table_2] (...)
create table [foo].[table_3] (...)
By placing all of the tables in one schema, I was able to grant permission at the schema level and that notion of permission trickled down to all of the tables contained therein.
i think this placement is because of mapping between security of users (roles and users) and schemas. not data structure of schema, like tables and columns and so on.
if you go to security tab -> Users-> double click on on of the users. you find that you can edit mapping between that user and owned or role membership of SCHEMAs.
im hopeful to be useful for u.
I have several tables with defined owner user in a SQL Server 2008 database
a_user.[table1]
a_user.[table2]
When I log in as admin I cannot query either table because it doesn't exist [in that user namespace].
How can I make admin or any other user be able to query table a_user.[table1] without referencing the user?
select * from a_user.table1 --> works
select * from table1 --> doesn't work, i'd like this to work
What you're looking at are schemas, not owners. The two concepts are distinct (from SQL Server 2005 onwards).
To resolve any table name, SQL Server will look in your default schema. By default, this will be dbo. The only way to access a table without specifying a schema name is for it to be in your default schema, or for you to add a synonym for the table within your default schema.