dblink
当我使用到远程服务器的命名连接或未命名连接并断开连接时,它似乎不起作用。如果我在 dblink() 中使用带有连接字符串的未命名连接,它工作正常。它似乎连接正常,但当我尝试使用它时我的连接不可用。关于如何使用命名连接进行此操作的任何想法?
Unnamed with connstr Works Fine:
SELECT testtable.*
FROM dblink('dbname=testdb port=5432 host=192.168.1.1 user=usr password=pw'
,'SELECT * FROM testtable')
AS testtable(testtable_id integer, testtable_name text);
返回:预期的两列。
命名不起作用:
连接:
SELECT dblink_connect('myconn'
,'dbname=testdb port=5432 host=192.168.1.1 user=usr password=pw');
返回:“确定”
查询:
SELECT testtable.* FROM dblink('myconn', 'SELECT * FROM testtable')
AS testtable(testtable_id integer, testtable_name text);
返回:
ERROR: could not establish connection
DETAIL: missing "=" after "myconn" in connection info string
********** Error **********
ERROR: could not establish connection
SQL state: 08001
Detail: missing "=" after "myconn" in connection info string
断开连接:
SELECT dblink_disconnect('myconn');
返回:
ERROR: connection "myconn" not available
********** Error **********
ERROR: connection "myconn" not available
SQL state: 08003
Unnamed with _connect 和 _disconnect 不起作用:
连接:
SELECT dblink_connect('dbname=testdb port=5432 host=192.168.1.1
user=usr password=pw');
返回:“确定”
查询:
SELECT testtable.* FROM dblink('SELECT * FROM testtable')
AS testtable(testtable_id integer, testtable_name text);
返回:
ERROR: connection not available
********** Error **********
ERROR: connection not available
SQL state: 08003
断开连接:
SELECT dblink_disconnect();
返回:
ERROR: connection not available
********** Error **********
ERROR: connection not available
SQL state: 08003
最佳答案
我有一个未命名连接的工作设置。
你在问题中所说的“未命名”实际上有一个名称参数。你混淆了那里的两个变体。在没有 'myconn'
的情况下尝试:
SELECT *
FROM dblink('SELECT * FROM testtable'
) AS testtable (testtable_id integer, testtable_name text);
请记住,建立连接和使用它必须发生在同一 session 中。
但老实说,我找不到您的命名连接有什么问题。我进行了一些测试,一切看起来都是正确的。我使用 PostgreSQL 9.1 进行了测试。
错误消息暗示 dblink 需要一个 connstr
。只有当第一个参数与现有的任何 connname
不匹配时才会发生这种情况简而言之:未找到连接 'myconn'
- 这让我怀疑你没有调用 dblink()
在与 dblink_connect()
相同的 session 中。
关于具有命名连接的 PostgreSQL dblink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9876573/