如何在 Powershell 中使用 PGPASS 文件来避免密码提示?

How to use PGPASS file in Powershell to avoid password prompt?

我不得不自动化我的 postgre 数据库备份。按照我的软件供应商的指示,我正在尝试使用 pg_dump.exe(见下文)文件进行备份,但提示我输入密码。

.\pg_dump.exe -h localhost -p 4432 -U postgres -v -b -F t -f "C:\Backup\Backup.tar" Repo

所以用谷歌搜索,发现根据“https://www.postgresql.org/docs/9.6/libpq-pgpass.html”,我可以在其中创建一个 pgpass.conf 文件'C:\Users\User1\AppData\Roaming\postgresql\pgpass.conf" 我做了。

然后我尝试在执行 pg_dump 命令之前将 pgpass.conf 文件的数据传递给 env 变量。但它不起作用。我仍然提示输入密码。这是 pgpass.conf 文件的内容:*:*:*:postgres:password

下面是我在 PowerShell 中尝试的代码,

   $Env:PGPASSFILE="C:\Users\User1\AppData\Roaming\postgresql\pgpass.conf"
   cd "C:\Program Files\Qlik\Sense\Repository\PostgreSQL.6\bin"
   .\pg_dump.exe -h localhost -p 4432 -U postgres -v -b -F t -f "C:\Backup\Backup.tar" Repo

为什么我仍然被要求输入密码?

当我键入以下代码时 $Env:AppData 我得到以下响应“C:\Users\User1\AppData\Roaming”

到处都有关于如何在 UNIX 或命令提示符下使用它的指导,但在 powershell 中没有。任何帮助表示赞赏。另外,如果您能指导我如何保护此密码文件,那就太好了。

使用密码提示我无法使用 windows 任务调度程序自动执行它。

我怀疑您有合适的解决方案,但是,作为通过命令提示符快速(但不安全)的解决方法,您可以使用变量 PGPASSWORD 保存密码,然后 运行 备份脚本。

示例可能类似于:

SET PGPASSWORD=password

cd "C:\Program Files\Qlik\Sense\Repository\PostgreSQL.6\bin" pg_dump.exe -h localhost -p 4432 -U postgres -b -F t -f "d:\qs_backup\QSR_backup.tar" QSR

我还没有让这该死的东西工作,但我确实找到了 this:

-w --no-password Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.

我在您对 pg_dump

的调用中没有看到 -w 参数

我使用 pg_hba 文件来允许“信任”连接,这是一种风险较高的方法,但我必须尽快完成工作。感谢您的时间和努力