Windows PSQL命令行:是否有一种方式允许无需密码登录?

25
我的目标是不需输入密码即可执行命令,有没有办法在Windows命令行中实现此目标?在Linux中,我可以将密码发送到标准输入流之类的地方,但我不确定是否适用于Windows。
谢谢!
9个回答

30

有两种方法:

  • 设置环境变量 PGPASSWORD,例如:set PGPASSWORD=yoursecretpassword
  • 按照文档描述使用密码文件 %APPDATA%\postgresql\pgpass.conf

在密码文件(我的位置为 C:\Users\Grzesiek\AppData\Roaming\postgresql\pgpass.conf)中使用文档中指定的格式。例如,要在本地 5432 服务器上将数据库 postgres 作为角色 postgres 连接,请添加:

localhost:5432:postgres:postgres:12345

我经过测试这个方法可以很好地运行(对我来说),但不要使用127.0.0.1。


@一个问题提问者:我更新了我的答案,现在它也适用于文件。 - Grzegorz Szpetkowski
告诉操作员如果使用PGPASSWORD,请不要使用-W选项;就这样。 - undefined

14
另一个便捷的选项(特别是如果您的PG服务器在您自己的客户端机器上运行,并且如果这对您来说不会带来任何安全问题)是在服务器上允许无密码登录("trust"身份验证模式)。
例如,pg_hba.conf 文件中的此行(在您的 DATA 目录中,典型位置为:C:\Program Files\PostgreSQL\9.0\data\)允许从您的本地机器无需密码访问。
host     all     all     127.0.0.1/32    trust

然后,连接

  psql.exe -h 127.0.0.1 -U postgres -w [YOUR_DB_NAME]

5

我知道这是一个老问题,但对于像我一样正在寻找答案的人来说,在Windows上很难找到解决方案。将以下内容放入.bat文件中,它将起作用(至少对我来说是这样)。更改目录到postres目录,设置环境变量PGPASSWORD,执行复制命令到csv文件,然后清除环境变量,最后返回根目录。

cd C:\Program Files\PostgreSQL\9.5\bin\

set PGPASSWORD=yourpassword


psql -d databasename -U yourusername -w -c "\COPY (select * from yourtable) TO 'c:/Users/yourdirectory/yourcsvfilename.csv' DELIMITER '|' CSV HEADER;"

set PGPASSWORD=

cd c:\

3

我知道这个问题现在有点老了,但我相信有更好的方法可以在Windows上实现安全、无需密码的PostgreSQL登录-SSPI

  1. Create a local or domain user account and PostgreSQL login with the same name.

  2. In pg_ident.conf, create a mapping:

    # MAPNAME   SYSTEM-USERNAME      PG-USERNAME
    SSPI        username@AUTHORITY   loginname
    

    Replace username with the Windows user name (this is the sAMAccountName attribute for domain accounts), and replace AUTHORITY with the computer name or short domain name. If you're not sure what to use for AUTHORITY, check the PostgreSQL log file. For a local account, this will be the computer name; for a domain account, it's probably the short domain name. Lastly, replace loginname with the PostgreSQL login name (to reduce confusion, I would recommend using the same name for both username and loginname).

  3. In pg_hba.conf, allow the logon; e.g.:

    # TYPE   DATABASE   USER        ADDRESS        METHOD
    host     all        loginname   127.0.0.1/32   sspi map=SSPI
    host     all        loginname   ::1/128        sspi map=SSPI
    
  4. If a domain account, set a SPN for it; e.g.:

    setspn -S POSTGRES/serverfqdn username
    
现在您可以使用指定的用户名登录Windows,运行psql.exe等操作而无需密码。

1

对于那些不想要 .pgpass 文件并希望在单个命令行实例中执行的人来说,这是另一种选择。

set "PGPASSWORD=yourpass" && createdb -h localhost -p 5432 -U postgres new_db_name

我参考了几乎所有与命令行相关的解决方案,最终在设置Windows 11中PGPASSWORD=yourpass时使用了"


0
步骤:
首先设置环境变量 PGPASSWORD
C:\Windows\system32>set PGPASSWORD=clave 然后执行以下命令 psql -d basededatos -U usuario
准备好了,

0
如果您能够使用Powershell,您可以像bash一样内联设置环境变量。
这应该可以工作:
$Env:PGPASSWORD='your-pass'; psql -U postgres

请注意在设置变量和实际命令之间的分号,这很重要,因为它们是内联但是两个不同的命令。

0

我在Postgres 15中使用以下方法使其工作:

psql -U postgres -h 127.0.0.1 -p 54322 -f some_file password=postgres

J


-3

我发现在这个链接里有另一个对我很有用的解决方案。它基本上会在你的命令开头设置PGPASSWORD,像这样:

PGPASSWORD=PASSWORD psql YOUR_CODE

2
这在bash中可以工作,但在Windows命令行中不行。 - Harabeck

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