psql CLI 客户端是否有类似 .ssh/config 的等价物来定义主机、用户、密码、数据库?

Does the psql CLI client have an equivalent like .ssh/config to define host, user, password, database?

.ssh/config 条目如下:

Host foo
  HostName 226.212.131.212
  User root

允许您使用 ssh foo 连接到服务器。

Postgres 的默认客户端 psql 具有允许存储密码的 .pgpass 文件。但是,仍然需要向客户端指示主机、用户名和数据库名的参数。

我正在寻找的是 ssh 配置的等价物 - 一种使用命名配置访问 postgres 服务器的方法。 psql foo。存在吗?

最接近的是 connection service file

来自手册:

The file uses an “INI file” format where the section name is the service name and the parameters are connection parameters;

[foo]
host=somehost
port=5432
user=arthur

定义服务后,您可以使用例如:

进行连接
psql "service=foo"

建立在

之上

要使其正常工作,请确保将您的配置文件保存到正确的位置。 从手册中,正确的路径是:

  • /etc/pg_service.conf
  • ~/.pg_service.conf

现在,如果您的文件已保存到该位置,您现在可以对其进行编辑,确保您拥有所有必需的设置。

[a_service]
 
host=host/orIP 
 
port=5432
 
dbname=xyz
 
user=paul
 
password=cde

你想加多少就加多少,和我们做的一样ssh_config

要使用它,您可以通过以下代码使用它:

分两步使用:

export PGSERVICE=a_service 然后 运行 psql

或在一条命令中使用:

psql "service=a_service"