如何在postgresql中将pg-service.conf服务用作dblink连接字符串

4

我有一个名为pg_service.conf的文件,其中包含以下配置:

pg_service.conf

[test-service]
host=localhost
port=5432
user=test-user
password=*****
dbname=test-table

运行以下查询时出现错误

查询

SELECT id, name 
FROM                                                                                                      
  DBLINK('service=test-service',
    'SELECT id, name FROM student' 
  ) AS sq0(id int, name text) order by 1;

错误

ERROR:  could not establish connection
DETAIL:  definition of service "test-service" not found

尝试运行以下代码:SELECT id, name FROM DBLINK('server=test-localhost port=5432 user=test-user password=*** dbname=dbname ', 'SELECT id, name FROM student' ) AS sq0(id int, name text) order by 1; - Vivek S.
1
如果我直接提供连接字符串('server=test-localhost port=5432 user=test-user password=*** dbname=dbname'),它可以正常工作。但是我想使用服务名称(test-service)代替连接字符串。 - Venkatesan
1个回答

0
你是否定义了 PGSERVICEFILEPGSERVICE 环境变量?如果没有,请在环境文件中按照以下方式定义它们,
export PGSERVICEFILE=${HOME}/.pg_service.conf   # location to the .pg_service.conf file
export PGSERVICE=test-service

如果您要使用多个模式,可以使用.pg_service.conf来定义数据库名称、IP和端口,以及.pgpass文件来定义用户名和密码。请注意,.pgpass应该在您的主目录中。同时,上述变量应该被设置。 示例 .pg_service.conf
[test-service]
host=localhost
port=5432
dbname=test-table

示例 .pgpass

localhost:5432:test-table:test-user1:******
localhost:5432:test-table:test-user2:******

然后您可以通过终端执行查询,命令为:psql --user=test-user1 -c "query"


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