PostgreSQL 上的同义词支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45238572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Synonym support on PostgreSQL
提问by gajendra kumar
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.
如何像在 Oracle 中一样在 PostgreSQL 上创建和使用同义词。我是否需要创建一些数据库链接或其他任何东西。我找不到关于这个主题的任何好的官方文档。
Edit 1
编辑 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.
实际上到目前为止,我有一个应用程序,它有两个独立的模块,连接两个不同的 oracle 数据库;一个模块需要访问其他的表,因此我们在 oracle 中通过 db 链接使用同义词。现在我们正在将应用程序迁移到 postgresql,所以我们需要同义词。
Edit 2When 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.
编辑 2当我说两个不同的 oracle 数据库时,这意味着它可以是两个不同的 oracle 实例或同一数据库的两个模式,它可以在应用程序中配置,并且应用程序必须支持这两种模式。
PostgreSQL version: 9.6.3
PostgreSQL 版本:9.6.3
采纳答案by gajendra kumar
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
最后,我使用外部数据包装器 postgres_fdw 使其工作,如下所示我有两个名为 dba 和 dbb 的数据库。dbb 有一个表用户,我需要在 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.
现在我可以在 dba 中执行所有选择/更新查询。
Approach 2:-Can be achieved by creating two schemas in same db, below are the steps:
方法2:-可以通过在同一个数据库中创建两个模式来实现,以下是步骤:
- 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;
- 创建两个模式 ex app_schema, common_schema。
授予访问权限:
GRANT CREATE,USAGE ON SCHEMA app_schema TO myuser; GRANT CREATE,USAGE ON SCHEMA common_schema TO myuser;
现在设置用户的搜索路径如下
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:
现在,myuser 可以看到 common_schema 中的表。例如,假设我们在 common_schema 中有一个表 user,在 app_schema 中有一个表 app,那么下面的查询将很容易运行:
select * from user;
select * from app;
This is similar to synonyms in oracle.
这类似于 oracle 中的同义词。
Note-Above queries will work PostgreSQL 9.5.3+
注意-以上查询适用于 PostgreSQL 9.5.3+
回答by 1ac0
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
.
我认为您不需要 Postgres 中的同义词,就像您在 Oracle 中需要它们一样,因为与 Oracle 不同,Postgres 中的用户和模式之间有明显的区别。这不是一对一的关系,多个用户可以轻松地使用多个模式,而无需通过利用 Postgres 的“搜索路径”功能来完全限定对象 - ? mydb.public.mytable
.
回答by Laurenz Albe
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.
如果这些表也应该位于 PostgreSQL 中的不同数据库中,那么您将使用外部数据包装器创建一个外部表。
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.
如果您使用 Oracle 同义词只是为了避免编写atable@dblink
,那么您不必在 PostgreSQL 中做任何事情,因为外部表的外观和感觉就像 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.
如果您将同义词用于其他目的,您可以设置search_path
为包含目标表所在的架构,或者您可以创建一个仅从目标表中选择所有内容的简单视图。