agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
public synonym
3+ messages / 3 participants
[nested] [flat]

* public synonym
@ 2016-01-07 20:17 Eugene Yin <eugeneymail@ymail.com>
  2016-01-07 20:27 ` Re: public synonym michael@sqlexec.com <michael@sqlexec.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Eugene Yin @ 2016-01-07 20:17 UTC (permalink / raw)
  To: pgsql-sql

PostgreSQL ver 9.4.5.  Linux OS.Application:  Web Based
Platform:      App Server (java) --> jdbc call --> Database Server (PostgreSQL)

I do know that PostgreSQL does not support the public synonym.  Now, for a user schema (let's call it MASTER_USER), if I coded in the stored function/procedure like the following, without using a public synonym to identify the table name: 
select user_name from user_info_table
then access it from the Java (web app) side via the JDBC call to the database, will that work?  
OR, 
I must use the identifier inside the sql, such as:
 select user_name from  MASTER_USER.user_info_table?


I come from the Oracle world, there I first create the public synonym for the table, then in the stored procedure I just directly reference the table with no need to identify the table with a schema name.  Like to know how it work under the PostgreSQL.
  
Thanks
Eugene

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: public synonym
  2016-01-07 20:17 public synonym Eugene Yin <eugeneymail@ymail.com>
@ 2016-01-07 20:27 ` michael@sqlexec.com <michael@sqlexec.com>
  2016-01-07 22:03   ` Re: public synonym Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 3+ messages in thread

From: michael@sqlexec.com @ 2016-01-07 20:27 UTC (permalink / raw)
  To: Eugene Yin <eugeneymail@ymail.com>; +Cc: pgsql-sql

You can infer the context by first setting the search_path variable.  
You can set it initially in your connection or do it for a database 
context or even a role context
SET search_path = MASTER_USER, public, pg_catalog;
ALTER DATABASE whatever  SET search_path = MASTER_USER, public, pg_catalog;
ALTER ROLE whoever SET search_path = MASTER_USER, public, pg_catalog;

Then you can continue to let the tables be non-qualified.   bye bye 
synonyms!

Regards
Michael
> Eugene Yin <mailto:eugeneymail@ymail.com>
> Thursday, January 7, 2016 3:17 PM
> PostgreSQL ver 9.4.5.  Linux OS.
> Application:  Web Based
>
> Platform:
>     App Server (java) --> jdbc call --> Database Server (PostgreSQL)
>
>
> I do know that PostgreSQL does not support the public synonym.Now, for 
> a user schema (let's call it MASTER_USER), if I coded in the stored 
> function/procedure like the following, without using a public synonym 
> to identify the table name:
>
> select user_name from user_info_table
>
> then access it from the Java (web app) sidevia the JDBC call to the 
> database, will that work?
>
> OR,
>
> I must use the identifier inside the sql, such as:
>
>  select user_name fromMASTER_USER.user_info_table?
>
>
> I come from the Oracle world, there I first create the public synonym 
> for the table, then in the stored procedure I just directly reference 
> the table with no need to identify the table with a schema name.  Like 
> to know how it work under the PostgreSQL.
>
>
> Thanks
>
> Eugene

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: public synonym
  2016-01-07 20:17 public synonym Eugene Yin <eugeneymail@ymail.com>
  2016-01-07 20:27 ` Re: public synonym michael@sqlexec.com <michael@sqlexec.com>
@ 2016-01-07 22:03   ` Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Adrian Klaver @ 2016-01-07 22:03 UTC (permalink / raw)
  To: michael@sqlexec.com <michael@sqlexec.com>; Eugene Yin <eugeneymail@ymail.com>; +Cc: pgsql-sql

On 01/07/2016 12:27 PM, michael@sqlexec.com wrote:
> You can infer the context by first setting the search_path variable.
> You can set it initially in your connection or do it for a database
> context or even a role context
> SET search_path = MASTER_USER, public, pg_catalog;
> ALTER DATABASE whatever  SET search_path = MASTER_USER, public, pg_catalog;
> ALTER ROLE whoever SET search_path = MASTER_USER, public, pg_catalog;

Just be aware that Postgres folds identifiers to lower case:

test=# create schema MASTER_USER;
CREATE SCHEMA
test=# \dn
     List of schemas
     Name     |  Owner
-------------+----------
  master_user | postgres
  public      | postgres

test=# create table MASTER_USER.test_tbl(id int);
CREATE TABLE

test=# \d master_user.test_tbl
  Table "master_user.test_tbl"
  Column |  Type   | Modifiers
--------+---------+-----------
  id     | integer |

UNLESS the tool you are using to create objects quotes the object name:

test=# create schema "MASTER_USER";
CREATE SCHEMA

test=# create table MASTER_USER.test_tbl(id int);
ERROR:  schema "master_user" does not exist

test=# create table "MASTER_USER".test_tbl(id int);
CREATE TABLE

test=# \d "MASTER_USER".test_tbl
  Table "MASTER_USER.test_tbl"
  Column |  Type   | Modifiers
--------+---------+-----------
  id     | integer |


More details at:

http://www.postgresql.org/docs/9.4/static/sql-syntax-lexical.html

4.1.1. Identifiers and Key Words





>
> Then you can continue to let the tables be non-qualified.   bye bye
> synonyms!
>
> Regards
> Michael
>> Eugene Yin <mailto:eugeneymail@ymail.com>
>> Thursday, January 7, 2016 3:17 PM
>> PostgreSQL ver 9.4.5.  Linux OS.
>> Application:  Web Based
>>
>> Platform:
>>     App Server (java) --> jdbc call --> Database Server (PostgreSQL)
>>
>>
>> I do know that PostgreSQL does not support the public synonym.Now, for
>> a user schema (let's call it MASTER_USER), if I coded in the stored
>> function/procedure like the following, without using a public synonym
>> to identify the table name:
>>
>> select user_name from user_info_table
>>
>> then access it from the Java (web app) sidevia the JDBC call to the
>> database, will that work?
>>
>> OR,
>>
>> I must use the identifier inside the sql, such as:
>>
>>  select user_name fromMASTER_USER.user_info_table?
>>
>>
>> I come from the Oracle world, there I first create the public synonym
>> for the table, then in the stored procedure I just directly reference
>> the table with no need to identify the table with a schema name.  Like
>> to know how it work under the PostgreSQL.
>>
>>
>> Thanks
>>
>> Eugene
>


-- 
Adrian Klaver
adrian.klaver@aklaver.com


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2016-01-07 22:03 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-01-07 20:17 public synonym Eugene Yin <eugeneymail@ymail.com>
2016-01-07 20:27 ` michael@sqlexec.com <michael@sqlexec.com>
2016-01-07 22:03   ` Adrian Klaver <adrian.klaver@aklaver.com>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox