从highgo数据库下通过dblink访问postgres数据库下的test表:
查询postgres数据库下的test表:
postgres=# select * from test;
id
----
1
2
3
(3 rows)
highgo数据库下创建dblink:
highgo=# create extension dblink ;
CREATE EXTENSION
highgo=# \dx
List of installed extensions
Name | Version | Schema | Description
--------------------+----------+--------------------+-----------------------------------------------------------------------------------------------
dblink | 1.2 | public | connect to other PostgreSQL databases from within a database
highgo=# select dblink_connect('test_dblink','dbname=postgres host=localhost port=5866 user=sysdba password=admin');
dblink_connect
----------------
OK
(1 row)
查询:
highgo=# select * from dblink('test_dblink','select * from test') as test(id int);
id
----
1
2
3
(3 rows)
highgo=# create view test_view as select * from dblink('test_dblink','select * from test') as test(id int);
CREATE VIEW
highgo=# select * from test_view;
id
----
1
2
3
(3 rows)
注意:
如果原生PG的/opt/postgresql-12.7/share/extension目录下没有dblink插件,需要到PG源码下安装:
cd /tmp/postgresql-12.7/contrib/dblink
make
make install