未提供密码/未找到位置参数 PostgreSQL - Powershell [英] No Password Supplied / Positional Parameter not found PostgreSQL - Powershell
问题描述
我正在尝试构建一个简单的脚本,该脚本将查询输出到 powershell 中的 csv.但是,它不断返回以下错误:
I am trying to build a simple script that outputs a query to a csv in powershell. However, it keeps returning the following error:
psql:fe_sendauth:未提供密码
psql: fe_sendauth: no password supplied
我尝试公开密码,因为这将是一个安全的环境,但看到了使用 pgpass 的建议,但没有关于如何实施的真正解释.
I tried exposing the password as this will be a safe environment, but have seen suggestions to use pgpass but no real explanation on how to implement.
Set-Location 'E:\Program Files\PostgreSQL\9.1\bin\';
SET 'PGPASSWORD = myPwd';
.\psql -U postgres -w MyDatabase
copy {'SELECT * FROM table';} TO 'C:\Users\e\Desktop\test1.csv' CSV DELIMITER ',';
推荐答案
SET
是 Set-Variable
的别名,但 Powershell 变量不是环境变量.要设置环境变量,您需要使用 $env:
范围.试试:
SET
is an alias for Set-Variable
, but Powershell variables are not environment variables. To set an environment variable, you need to use the $env:
scope. Try:
$env:PGPASSWORD = 'myPwd';
另请参阅此处,了解有关环境变量的更多信息.
See also here for more on environment variables.
此外,我认为您不能像在 PowerShell 中那样将原始输入放在命令行上.我认为它会将事物视为单独的命令,但我可能是错的.
Also, I don't think you can get away with putting raw input on the command line like that in PowerShell. I think it will treat things as separate commands, but I could be wrong.
您可能还想在调用 psql 时使用命令开关 (-c
) 和 PowerShell 停止解析符号 (--%
) 以防止 PowerShell 解析您的命令字符串:
You may also want to use the command switch (-c
) and the PowerShell stop parsing symbol (--%
) when you call psql to prevent PowerShell from parsing your command string:
.\psql --% -U postgres -w MyDatabase -c "copy {'SELECT * FROM table';} TO 'C:\Users\e\Desktop\test1.csv' CSV DELIMITER ',';"
或者将命令设置为带有 here-strings 的变量并将其通过管道传输到 psql:
Or set the commands to a variable with here-strings and pipe that to psql:
$query = @'
copy {'SELECT * FROM table';} TO 'C:\Users\e\Desktop\test1.csv' CSV DELIMITER ',';
'@
$query | .\psql -U postgres -w MyDatabase
或者其他十几种调用可执行文件的方法.
Or about a dozen other ways to call an executable.
这篇关于未提供密码/未找到位置参数 PostgreSQL - Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!