今天群里一个哥们发了个错误信息:
ERROR: cross-database references are not implemented: "public.test.id"
********** 错误 **********
ERROR: cross-database references are not implemented: "public.test.id"
SQL 状态: 0A000
看到贴出的SQL语句时就发现他的错误了
comment on table public.test is '测试表';
comment on table public.test.id is '测试ID';
..
给字段加备注的时候应该是column,而不是table。修改完备注SQL就不会报错。
报这种错误,得须要提醒一下,postgresql的体系结构认为不一样的数据库是独立的,因此并不支持跨库查询。并且执行一个查询时,postgresql首先会检查自身的dbname(通常链接的当前DB名,可不写),而后是schema(由于一个DB里可包含多个schema,默认链接是public),再最后才是数据库对象(table,view,function...)。
因此当出现cross-database错误时咱们就能够认为postgres认为用户作了一次非规范的跨库操做了。
回过头来想想,什么状况下会报这种错误呢,我整理了一下,有如下这么几种状况:
测试环境:
postgresql 9.1.2
postgres=# \l
资料库列表
名称 | 拥有者 | 字元编码 | Collate | Ctype | 存取权限
-----------+----------+----------+---------+-------+-----------------------
d_kenyon | postgres | UTF8 | C | C |
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(4 行记录)
postgres=# \d
关联列表
架构模式 | 名称 | 型别 | 拥有者
----------+----------------+--------+----------
public | pgweb | 资料表 | postgres
public | t_kenyon | 资料表 | postgres
public | tbl_order_item | 资料表 | postgres
public | tbl_orders | 资料表 | postgres
public | tbl_product | 资料表 | postgres
(5 行记录)
postgres=# select current_user;
current_user
--------------
postgres
(1 行记录)
postgres=# \c d_kenyon
You are now connected to database "d_kenyon" as user "postgres".
d_kenyon=# \d
关联列表
架构模式 | 名称 | 型别 | 拥有者
----------+-------+--------+----------
public | test | 资料表 | postgres
public | test2 | 资料表 | postgres
(2 行记录)
1.真正地作了一次跨库查询
d_kenyon=# select *from postgres.public.t_kenyon limit 1;
ERROR: cross-database references are not implemented: "postgres.public.t_kenyon
"
第1行select *from postgres.public.t_kenyon limit 1;
^
d_kenyon=#
2.没有作跨库查询,可是pg误认为你作了一次跨库操做
postgres=# \d
关联列表
架构模式 | 名称 | 型别 | 拥有者
----------+----------------+--------+----------
public | pgweb | 资料表 | postgres
public | t_kenyon | 资料表 | postgres
public | tbl_order_item | 资料表 | postgres
public | tbl_orders | 资料表 | postgres
public | tbl_product | 资料表 | postgres
(5 行记录)
postgres=# \d t_kenyon
资料表 "public.t_kenyon"
栏位 | 型别 | 修饰词
--------+---------+--------
id | integer |
t_name | text |
postgres=# select * from public.t_kenyon limit 1;
id | t_name
----+--------
1 | kenyon
(1 行记录)
postgres=# select * from public.t_kenyon.id limit 1;
ERROR: cross-database references are not implemented: "public.t_kenyon.id"
第1行select * from public.t_kenyon.id limit 1;
postgres=# select * from public.t_kenyon.22 limit 1;
ERROR: syntax error at or near ".22"
第1行select * from public.t_kenyon.22 limit 1;
^
postgres=# select * from public.t_kenyon.x22 limit 1;
ERROR: cross-database references are not implemented: "public.t_kenyon.x22"
第1行select * from public.t_kenyon.x22 limit 1;
第二种场景里面还有多种方式会触发,好比最开始提到的comment,insert...select...也有可能,第二种场景里有一个颇有意思的发现,当最后一个实心点后面跟的是数字时并不报跨库错误,而是语法错误。
扩展: 1.schema是 postgresql里一个相对特别的数据库对象,能够简单将理解成是一个容器,相似oracle的一个namespace,可是更灵活,可对schema授相应的用户权限来控制; 2.除此以外,能够将DB中的数据按相应的功能作水平切分,存放到不一样的schema里面; 3.不一样的用户能够设置不一样的默认schema,可参考search_path这个变量; 4.目前版本JDBC链接postgresql时须要先链接上来,而后执行set search_path to xxx,否则可能取不到特定schema下的数据。