PostgreSQL上的同义词支持 [英] Synonym support on PostgreSQL

查看:1791
本文介绍了PostgreSQL上的同义词支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如在Oracle中一样,如何在PostgreSQL上创建和使用同义词。我是否需要创建一些数据库链接或其他任何东西。我找不到有关此主题的任何正式文档。

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.

编辑1

实际上,到目前为止,我有一个应用程序,该应用程序具有两个单独的模块,这些模块可以连接两个不同的oracle数据库;一个模块需要访问其他模块的表,因此我们在oracle中通过db链接使用同义词。现在我们正在将应用程序迁移到PostgreSQL,因此我们需要同义词。

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.

编辑2
当我说两个不同的oracle数据库时,这意味着它可以是两个不同的oracle实例,也可以是同一数据库的两个模式,可以在应用程序中配置,并且应用程序必须支持两种模式。

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版本:9.6.3

PostgreSQL version: 9.6.3

推荐答案

方法1:-

最后我使用外部数据包装器postgres_fdw使其工作,如下
,我有两个名为dba和dbb的数据库。 dbb有一个表用户,我需要在dba中访问它

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

现在我可以在dba中执行所有选择/更新查询。

Now i can execute all select/update queries in dba.

方法2:-
可以通过在同一数据库中创建两个模式来实现,步骤如下:

Approach 2:- Can be achieved by creating two schemas in same db, below are the steps:


  1. 创建两个方案,例如app_schema和common_schema。

  2. 授予访问权限:

  1. create two schemas ex app_schema, common_schema.
  2. 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;
    


  • 现在,myuser可以看到common_schema中的表。例如,假设我们在common_schema中有一个表用户,在app_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;
    

    这类似于oracle中的同义词。

    This is similar to synonyms in oracle.

    注意-以上查询将适用于PostgreSQL 9.5.3 +

    Note- Above queries will work PostgreSQL 9.5.3+

    这篇关于PostgreSQL上的同义词支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆