【发布时间】:2017-09-07 21:32:22
【问题描述】:
编辑:我已经解决了这个问题。剧透,它与 psql 或 fdw 无关。这是一个 DNS 问题,因为我在未配置内部 DNS 服务器的 docker 机器上运行本地数据库。
我正在尝试在我的数据库中创建一个外部表(来自另一个 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 数据库。我创建外部表的脚本非常简单,并且以文档 here 为蓝本。
-- 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左右... -
我认为这是一个 DNS 问题。我可以从我的主机操作系统 (Ubuntu) ping 我的内部服务器,但不能使用
docker exec。
标签: postgresql-9.6 foreign-data-wrapper postgres-fdw