创建使用postgres_fdw的外部表时出现主机名翻译错误。

3

编辑:我已经解决了这个问题。剧透一下,这与psql或fdw没有任何关系。它是一个DNS问题,因为我在运行本地数据库的docker机器上没有配置我们内部的DNS服务器。

我正在尝试在我的数据库中创建一个外部表(从另一个postgres数据库),然而,当我运行select语句时,外部数据包装器会显示无法解析提供的主机名:

ERROR:  could not connect to server "pgs3"
DETAIL:  could not translate host name "db7.ap.int.unavco.org" to address: Name or service not known

那么我的主机名有什么问题?我可以使用psql -U myuser -W -h db7.ap.int.unavco.org -d pgs3连接到pgs3数据库。我创建外部表的脚本非常简单,模仿了文档这里

-- Drop everything if the fdw exists already
DROP EXTENSION IF EXISTS postgres_fdw CASCADE;

-- Add the postgres_fwd extension
CREATE EXTENSION postgres_fdw WITH SCHEMA public;

-- Create foreign server object
CREATE SERVER pgs3 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'db7.ap.int.unavco.org', dbname 'pgs3', port '5432');

-- Create user mapping object to authenticate
CREATE USER MAPPING FOR postgres SERVER pgs3 OPTIONS (user 'afakeuser', password 'afakepassword');

-- Create the foreign table, ensuring the types and NOT NULL rules match the target table
-- The target table only has two columns to keep things simple
CREATE FOREIGN TABLE analysis_type (
    type_id smallint,
    type varchar NOT NULL
)
SERVER pgs3;

-- Try a select
SELECT
*
FROM
analysis_type;

-- Get an error...

你是在服务器端还是客户端运行 psql -U myuser -W -h db7.ap.int.unavco.org -d pgs3?客户端可能只需使用 /etc/hosts 等等... - Vao Tsun
我认为这是一个DNS问题。 我可以从我的宿主操作系统(Ubuntu)ping通内部服务器,但不能使用docker exec命令。 - medley56
1个回答

1
作为最近遇到这个问题的人,我发现了以下解决方法。这是一个DNS问题,因为我能够使用原始服务器IP地址并成功建立连接。我可以从我的应用程序服务器ping通该域名,但建立连接仍然失败。
在我这种情况下的原因是,我们有单独的应用程序和数据库服务器。我们的数据库服务器也需要能够解析我指定的域名。一旦我在数据库服务器的/etc/hosts中映射了该域名并重新启动了数据库服务,我就可以使用该域名作为主机并建立连接了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接