postgres 跨數據庫查詢
dblink 的安裝
linux
#cd postgres源碼安裝目錄/contrib/dblink
# make
# make install
注意: 如果你在安裝了postgres后執行了 make clean, make distclean, 那你可能需要重新 ./configure make make install 一下
windows
windows 默認是安裝的
dblink模塊加載
PostgreSQL 有很多外部模塊可以加載,例如 dblink, pg_buffercache 等,在 9.1 版本
以前,只要對應的 postgresql-contrib 已經安裝,只需要將對應的 sql 文件導入到目標庫
即可,例如,要在 數據庫 skytf 里安裝 dblink 模塊,只需要執行以下操作就行;
cd $PGHOME/share/contrib
psql -d skytf -U postgresql -f dblink.sql
導入成功之后,那么 dblink 模塊即加載成功。
注意!!
在 9.1 版本以后,模塊加載環節 PostgreSQL 提供命令 "CREATE EXTENSION" 來替代以上操作。
通過執行 CREATE EXTENSION dblink來加載 dblink
dblink 使用:
關于dblink, dblink_connect, dblink_disconnect 請參考手冊
http://www.postgresql.org/docs/9.1/static/dblink.html
例1
select dblink_connect('連接名','host=192.168.1.27 port=1921 dbname=testdb user=test_user password=test_user' );
host, port, user等 可以跟據情況省略掉
例2
select * from tb1 inner join dblink('dbname=db2', 'select id from tb2 where id=\'20120623\'') as acc(id int) on tb1.id = acc.id order by tb1.id;
參考網站:
http://blog.sina.com.cn/s/blog_538d55be01010clc.html
http://francs3.blog.163.com/blog/static/4057672720108401139868/
http://www.postgresql.org/docs/9.1/static/dblink.html
http://zhenghaoju700.blog.163.com/blog/static/135859518201251382628663/