如何使用 dblink 从 Postgres 连接到远程 SQL Server?
我有一个 Postgres SQL 查询,可以检索除一个未存储的字段之外的所有我需要的数据。我必须连接到 SQL Server 数据库才能检索这个附加字段,这是我使用 dblink 进行的。我在其他一些查询中使用了 dblink,但仅将 Postgres 连接到 Postgres。
SELECT
cm.course_id AS "EXTERNAL_COURSE_KEY",
cm.course_id AS "COURSE_ID",
cm.course_name AS "COURSE_NAME",
cm.start_date AS "START_DATE",
'' AS "END_DATE",
cm.available_ind AS "AVAILABLE_IND",
CASE
WHEN cm.row_status = '0' THEN
'ENABLED'
WHEN cm.row_status = '1' THEN
'UNDEFINED'
WHEN cm.row_status = '2' THEN
'DISABLED' ELSE'DELETED'
END ROW_STATUS,
'TEMPLATE ID' AS "TEMPLATE_COURSE_KEY",
CASE
WHEN cm.duration = 'C' THEN
'CONTINUOUS'
WHEN cm.duration = 'R' THEN
'RANGE'
END DURATION
FROM
course_main cm
CROSS JOIN dblink ( 'host=<HOST> port=<PORT NUM> dbname=<DB NAME> user=<USERNAME> password=<PASSWORD>',
'SELECT mle_id, template_id from mle_object;' ) -- accessing sql server db
AS course_templates ( mle_id VARCHAR(255), template_id VARCHAR(255) )
WHERE
cm.course_id = 'I3016-AMBS-60037-1192-06M-027386'
当我运行查询时,出现以下错误。
错误:无法建立连接详细信息:无法连接到 服务器:连接超时 服务器是否在主机上运行 “[HOST]”([IP 地址])并接受 TCP/IP 端口 [PORT NUM] 上的连接
我可以在单独的查询中从此 sql server 数据库检索数据,但不能使用 dblink。
任何帮助表示赞赏。
谢谢
I have a Postgres SQL query that retrieves all the data I need apart from one field, which is not stored. I have to connect to a SQL Server database to retrieve this additional field, which I am doing using dblink. I've used dblink in a few other queries, but only connecting Postgres to Postgres.
SELECT
cm.course_id AS "EXTERNAL_COURSE_KEY",
cm.course_id AS "COURSE_ID",
cm.course_name AS "COURSE_NAME",
cm.start_date AS "START_DATE",
'' AS "END_DATE",
cm.available_ind AS "AVAILABLE_IND",
CASE
WHEN cm.row_status = '0' THEN
'ENABLED'
WHEN cm.row_status = '1' THEN
'UNDEFINED'
WHEN cm.row_status = '2' THEN
'DISABLED' ELSE'DELETED'
END ROW_STATUS,
'TEMPLATE ID' AS "TEMPLATE_COURSE_KEY",
CASE
WHEN cm.duration = 'C' THEN
'CONTINUOUS'
WHEN cm.duration = 'R' THEN
'RANGE'
END DURATION
FROM
course_main cm
CROSS JOIN dblink ( 'host=<HOST> port=<PORT NUM> dbname=<DB NAME> user=<USERNAME> password=<PASSWORD>',
'SELECT mle_id, template_id from mle_object;' ) -- accessing sql server db
AS course_templates ( mle_id VARCHAR(255), template_id VARCHAR(255) )
WHERE
cm.course_id = 'I3016-AMBS-60037-1192-06M-027386'
When I run the query, I get the error below.
ERROR: could not establish connection DETAIL: could not connect to
server: Connection timed out Is the server running on host
"[HOST]" ([IP ADDRESS]) and accepting TCP/IP
connections on port [PORT NUM]
I can retrieve data from this sql server database in a separate query, but not using dblink.
Any help is appreciated.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能。 dblink 扩展只能在 Postgres 实例之间使用。
如果需要访问 SQL Server 表,则必须使用外部数据包装器。
SQL Server 有 一个(如果我没记错的话,我只在 Linux 上工作) 。
安装包装器后,您可以创建一个 外部表 并加入在你的查询中。
You can't. The dblink extension is only usable between Postgres instances.
If you need to access a SQL Server table, you will have to use a foreign data wrapper.
There is one for SQL Server (I only works on Linux if I'm not mistaken).
Once the wrapper is installed you can create a foreign table and join to that in your query.