AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 307567
Accepted
s.k
s.k
Asked: 2022-02-17 03:25:27 +0800 CST2022-02-17 03:25:27 +0800 CST 2022-02-17 03:25:27 +0800 CST

为什么使用 pg_service.conf 时 psql 不要求输入数据库密码

  • 772

我已经打开了一个 SSH 隧道来连接到远程服务器,如下所示:

$ ssh -f -N -L 5433:127.0.0.1:5432 username@servername

这条隧道已精确开通如下(如 所示ps aux | grep ssh):

ssh -f -N -L 5433:127.0.0.1:5432 username@servername

我确实有一个~/.pg_service.conf:

[my-pg-service]
    host=127.0.0.1
    port=5433
    dbname=mydatabase
    user=pguser

# just append the .pgpass file here:
[my-pg-service-2]
    host=127.0.0.1
    port=5433
    dbname=mydatabase
    user=pguser
    passfile=~/.pgpass

运行时:

$ psql service=my-pg-service

它目前(并且令人惊讶地)无需询问数据库密码即可连接!(可能它存储在某种缓存中,因为我在该命令之前已经使用过它?)

但是在使用时psql service=my-pg-service-2它实际上要求输入数据库密码。
我希望它会以相反的方式表现!

我的~/.pgpass( chmod 0600) 文件如下所示:

#hostname:port:database:username:password

# Remote pg database on server servername when using an SSH tunnel (5433)
127.0.0.1:5433:mydatabase:pguser:8+k3&4d2ihs1=&gp!*y)62xoh+^^z$&*ino!66jj()(yw@o36

请注意,此命令还要求输入数据库密码:

$ psql -d postgres://pguser@localhost:5433/mydatabase

但这不是:

$ psql -d postgres://[email protected]:5433/mydatabase

(我只更改localhost为127.0.0.1)。

这正是因为文件中没有以开头localhost的行.pgpass:

localhost:5433:mydatabase:pguser:8+k3&4d2ihs1=&gp!*y)62xoh+^^z$&*ino!66jj()(yw@o36

如果我添加此行,psql则在前面两种情况下都无需提示输入密码即可连接,但我仍然遇到 pg 服务的问题,例如,当指定passfile=~/.pgpass它时要求输入密码,而当没有时,它不会。

有人可以解释这种行为以及我做错了什么吗?
这可能与过于宽泛的服务器端配置有关吗?

以防万一,数据库在远程服务器上被 dockerized。它使用了官方的 postgis docker 镜像( 13:3.2),它呈现了这些默认特性:

$ docker run \
  --rm \
  --name postgis \
  -e POSTGRES_DB=postgres \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=changeme \
  -d postgis/postgis:13-3.2

$ docker exec -it postgis bash -c "tail -n 21 /var/lib/postgresql/data/pg_hba.conf"

# CAUTION: Configuring the system for local "trust" authentication
# allows any local user to connect as any PostgreSQL user, including
# the database superuser.  If you do not trust all your local users,
# use another authentication method.

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

host all all all md5

操作系统:Ubuntu 21.10
PG:14

文档:
https ://www.postgresql.org/docs/14/libpq-pgservice.html
https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-PARAMKEYWORDS
https://www。 postgresql.org/docs/14/libpq-pgpass.html

postgresql
  • 2 2 个回答
  • 486 Views

2 个回答

  • Voted
  1. Best Answer
    Laurenz Albe
    2022-02-17T07:31:29+08:002022-02-17T07:31:29+08:00

    这很容易解释。

    如果您不指定passfile,则密码取自.pgpass您的主目录,因此不会提示您输入密码。

    如果您指定passfile=~/.pgpass,则 PostgreSQL 无法找到该文件,因为它不理解波浪号(这是一个 shell 功能,并且您没有使用 shell),因此它会提示您输入密码。

    • 2
  2. jjanes
    2022-02-17T07:35:35+08:002022-02-17T07:35:35+08:00

    服务文件不理解波浪号来表示您的主目录。它将它作为文件名中的文字字符。所以那个文件不存在,你基本上取消了那个文件。(出于某种原因,没有警告)。

    • 2

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve