使用postgresql和dblink连接两个数据库

时间:2013-12-22 20:27:47

标签: sql postgresql dblink

我有以下数据库:

server_a {
    a_t1 (a_t1_a1, a_t1_a2);
    a_t2 (a_t2_a1, a_t2_a2);
}
server_b {
    b_t1 (b_t1_a1, b_t1_a2);
    b_t2 (b_t2_a1, b_t2_a2);
}

attribute 1 = serial not null (primary)
attribute 2 = character varying(50)

我必须在第一个到第二个数据库中加入一个表

这是我推测的sql:

Select * from a_t1 full outer join dblink('dbname=server_b','select * from b_t1') 
where a_t1.a_t1_a1>1

但是我收到了这个错误:

ERROR:  syntax error at or near "where"
LINE 1: ...in dblink('dbname=server_b','select * from b_t1') where a_t1...
                                                             ^
********** Error **********

ERROR: syntax error at or near "where"
SQL state: 42601
Character: 83

1 个答案:

答案 0 :(得分:2)

试试这样。

为远程表创建别名并执行查询。

Select * from a_t1 full outer join dblink('dbname=server_b','select * from b_t1')  as tmp_b_t1 ( a1 serial not null (primary) , a2 character varying(50) )
where a_t1.tmp_b_t1>1