【问题标题】:How to connect to localhost with postgres_fdw?如何使用 postgres_fdw 连接到 localhost?
【发布时间】:2015-04-27 16:06:55
【问题描述】:

我的想法是我有一个名为 northwind 的本地数据库,并且使用 postgres_fdw 我想在 localhost 上连接另一个名为 test 的数据库(远程连接模拟,例如当我的数据库中的表被更新时,在其他数据库中执行某些操作,例如保存到历史记录等。)。所以我打开 psql 控制台并输入:

CREATE SERVER app_db 
FOREIGN DATA WRAPPER postgres_fdw 
OPTIONS (dbname 'test', host 'localhost:5432');

正如我在A Look at Foreign Data Wrappers 链接中找到的。接下来我也跟着教程走:

CREATE USER MAPPING for postgres
SERVER app_db 
OPTIONS (user 'postgres', password 'postgres');

(我假设用户和密码应该与我使用的相同,例如在 Hibernate 中访问数据库,在我的情况下,教程中的 current_userpostgres,因为这是我的 PgAdmin 中存在的唯一角色三)。 然后我创建了外表:

CREATE FOREIGN TABLE groups
(
  id serial NOT NULL,
  name character varying(255) NOT NULL,
  version integer DEFAULT 0
)
 SERVER app_db OPTIONS (table_name 'groups')

没关系,我可以在 PgAdmin III 的 schema/foreign tables 中找到它。但是当我尝试SELECT * FROM groups 时,我得到了:

********** ERROR**********

ERROR: could not connect to server "app_db"
SQL State: 08001

可能是因为当我CREATE SERVER app_db.. 我不使用本地主机服务器名称时?我不能,因为它的名称带有空格(PostgreSQL 9.3),并且在创建时会导致一些奇怪的问题。先感谢您。 更新:即使我在 localhost 上创建另一个名为`app_db 的服务器,它也不起作用。

【问题讨论】:

    标签: database postgresql postgresql-9.3 pgadmin foreign-data-wrapper


    【解决方案1】:

    经过多次尝试,我可能找到了一种正确的连接方式:

    CREATE SERVER app_db 
    FOREIGN DATA WRAPPER postgres_fdw 
    OPTIONS (dbname 'test', port '5432', host 'localhost');
    

    然后:

    CREATE USER MAPPING for postgres
    SERVER app_db 
    OPTIONS (user 'postgres', password 'postgres');
    

    然后:

    CREATE FOREIGN TABLE groups
    (
      id serial NOT NULL,
      name character varying(255) NOT NULL,
      version integer DEFAULT 0
    )
     SERVER app_db OPTIONS (schema_name 'public', table_name 'groups')
    

    但是有没有办法检查它是否真的是“远程”连接?因为服务器在同一个localhost上,不知道能不能确定。

    【讨论】:

    • 很抱歉在这里提问,但我一步一步按照您的建议进行操作,当我执行选择 ERROR: could not connect to server "omnes_db" DETAIL: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5431? could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5431? 时出现此错误,pg_hba.congpostgresql.conf 文件已正确设置以允许删除安全连接。真的不知道该怎么办
    • 更新:以上答案非常适合我的情况stackoverflow.com/a/39259965/903998。要访问位于同一 postgres 实例中的另一个数据库,您可以省略服务器的主机和端口以及用户映射的密码。可能您必须通过授予至少“选择”权限来允许访问外部表。
    【解决方案2】:

    您可以使用 Unix 域套接字而不是 TCP 连接进行连接,以获得更简单的配置和更好的性能(仅限 Linux/Unix,Windows 不支持)。

    CREATE SERVER app_db
    FOREIGN DATA WRAPPER postgres_fdw
    OPTIONS (dbname 'test');
    

    此外,如果启用了peer 身份验证(默认),您可以省略密码。

    CREATE USER MAPPING for postgres
    SERVER app_db 
    OPTIONS (user 'postgres');
    

    注意: peer 身份验证只能用于postgres 用户,因为FDW 连接是由以系统用户postgres 运行的服务器后端创建的。

    另外,出于安全原因,postgresq_fdw 仅允许具有SUPERUSER 权限的客户端进行peer 身份验证。要允许受限用户使用 FDW,您必须使用password 身份验证,如this answer 中所述

    【讨论】:

      猜你喜欢
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多