PostgreSQL报跨库异常及解决一例

今天群里一个哥们发了个错误信息:
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下的数据。

转载于:https://my.oschina.net/Kenyon/blog/90863

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL的dblink扩展提供了一种简单的方法,通过使用函数来连接到其他数据库,并执行查询。这对于需要查询多个数据库的应用程序非常有用。 以下是使用dblink进行跨库查询的步骤: 1. 安装dblink扩展 在使用dblink之前,需要先安装该扩展。可以通过以下命令在PostgreSQL中安装dblink扩展: ``` CREATE EXTENSION dblink; ``` 2. 创建连接 使用dblink连接到其他数据库需要使用`dblink_connect()`函数。该函数需要传递连接字符串和连接名称作为参数。连接字符串指定要连接的数据库的主机名、端口、数据库名称、用户名和密码。例如: ``` SELECT dblink_connect('dbname=mydb host=myhost port=5432 user=myuser password=mypass', 'myconn'); ``` 此命令将连接到名为`mydb`的数据库,该数据库位于主机`myhost`上,端口为5432,使用用户名`myuser`和密码`mypass`进行身份验证。连接名称为`myconn`。 3. 执行查询 连接到其他数据库后,可以使用`dblink()`函数执行查询。该函数接受两个参数:连接名称和查询字符串。例如: ``` SELECT dblink('myconn', 'SELECT * FROM otherdb.public.users'); ``` 此命令将从名为`otherdb`的数据库中选择`public.users`表中的所有行,并返回结果。 4. 关闭连接 使用`dblink_disconnect()`函数关闭连接。例如: ``` SELECT dblink_disconnect('myconn'); ``` 此命令将关闭名为`myconn`的连接。 需要注意的是,使用dblink进行跨库查询可能会影响性能,因为查询将在不同的数据库之间传输数据。因此,应该仅在必要时才使用它,并注意优化查询以减少数据传输。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值