• Postgres不同数据库间访问


    Postgres中不同数据库间无法直接访问,比如有数据库test1和test2,每个数据库中都有一个表为tablea;我使用的操作系统为win8.1,postgres版本为9.3.6;使用pgAdminIII工具访问postgres;

    image

    分别在两个数据库(test1,test2)中创建tablea;

    test1数据库创建tablea并插入数据:

    create table tablea(id int,name varchar(10));

    insert into tablea values

    (1,’a’),(2,’b’);

    test2数据库创建tablea并插入数据:

    create table tablea(id int,name varchar(10));

    insert into tablea values

    (1,’c’),(2,’d’);

    从test1数据库中分别访问test1和test2数据库中的表

    image

    image

    所报的错误为:未实现数据库关联:”test2.public.tablea”  SQL 状态:0A000

    我上面写表是按照:数据库.模式.表名

    SQL Server中模式是dbo,访问的时候使用select * from test2.dbo.tablea;个人感觉pgAdminIII和SSMS类似,或者说各个数据库窗口管理工具都类似,Mysql Workbench也类似。

    现在先截个图,看看test1数据可test2数据库模式下面的函数个数(全部是0)

     image

    在test1数据库中打开SQL窗口,输入create extension dblink;如下图

    image

    再在test数据库上刷新下,就会看到模式—public--函数,括号内数字不是0了

    image

    然后在SQL窗口中建立连接和查询

    select dblink_connect(‘t_connect’,’dbname=test2 host=localhost port=5432 user=postgres password=postgres’);

    select * from dblink(‘t_connect’,’select * from tablea’)  as t2(id int,name varchar(10));

    image

    上面的select语句其实是利用了dblink(text,text)和dblink_connect(text,text)函数

    两个数据库中表的连接其实也类似;还是在test1数据库的SQL窗口查询

    select a.*,b.name from tablea a inner join

    (select * from dblink('t_connect','select * from tablea') as t2(id int,name varchar(10))) b

    on a.id=b.id

    image

    就是把这个查询select * from dblink('t_connect','select * from tablea') as t2(id int,name varchar(10))当成一个表就可以了。

    很类似SQL Server中链接服务器,查询链接服务器的sql,之前随笔中应该写过SQL Server链接的MySQL,通过SSMS查询MySQL中的数据。

    Postgres以前我也没接触过,不过工作中需要使用Postgres,有应用的需求,总要解决!

  • 相关阅读:
    客户端性能优化
    session、cookie、token的区别
    java 动态代理模式(jdk和cglib)
    java 静态代理模式
    java ReentrantLock 公平锁 非公平锁 测试
    java ReentrantLock结合条件队列 实现生产者-消费者模式 以及ReentratLock和Synchronized对比
    VS2010 开发 VB6.0 activeX控件 dll
    C++CLI语法 在项目中的使用
    word 内容控件属性编辑
    VC调用静态库、动态库
  • 原文地址:https://www.cnblogs.com/cnmarkao/p/4321532.html
Copyright © 2020-2023  润新知