Postgres:使用 dblink 建立远程连接

Postgres: Using dblink to make remote connection

我们从承包商那里获得了 Postgres 数据库凭据(启用了 SSL),这使我们能够使用 pdAdmin 3 连接到数据库。该数据库是只读的(不能 pg_dump)我们和承包商基本上不会授予我们更多特权。

我们需要从这个远程数据库中获取一些数据到我们的本地数据库中。所以想用dblink来完成这个任务。

我在 psql 上 运行 这个:

insert into shifts select * from dblink('hostaddr=remote_addr port=9000 dbname=production user=user password=passwd', 'select user_id, location_id from shifts') as t1(user_id integer, location_id integer);

然后我得到:

ERROR: password is required DETAIL: Non-superuser cannot connect if the server does not request a password. HINT: Target server's authentication method must be changed.

因为我是 Postgres 和 dblink 的新手,我不知道为什么它抱怨没有密码。我想知道,要进行 dblink 连接,远程数据库是否需要授予更多权限?

如果 pdAdmin 3 能够使用凭据连接到远程数据库,我应该怎么做才能使 dblink 正常工作?

谢谢!

是的,只有超级用户可以为您提供通过 DBLINK 连接的设施

只要运行这条命令就可以连接到数据库

SELECT dblink_connect('myconn', 'dbname=postgres');
SELECT dblink_connect('myconn', 'hostaddr=remote_addr port=9000 dbname=production user=user password=passwd');

在dbname

后面输入你要连接的数据库名称即可

您可以连接

dblink_connect_u

dblink_connect_u() is identical to dblink_connect(), except that it will allow non-superusers to connect using any authentication method.

postgres 站点上的

Link 是 dblink

对于超级用户,要求他们创建扩展

CREATE EXTENSION dblink;

用于您的数据库或架构。