PowerShell脚本可以从Postgres表中读取数据

发布于 2025-01-23 20:57:19 字数 384 浏览 3 评论 0原文

我正在创建一个PowerShell脚本来读取Postgres DB
的数据 但是psql.exe命令在 psql.exe行之后,控制台要求密码
而且只有只有当我按ctrl+c执行其他行时,

我尝试使用start-job,但是我无法读取我的选择命令的输出,仅返回以下行
Job启动:System.Management.Automation.promotingjob指出工作已经开始,

我也尝试了Invoke-command,但这也无济于事。

任何人都可以帮助我使用一个简单的示例,该示例说明如何输入psql.exe CMD的密码以及如何从选择CMD读取输出

I am creating a powershell script to read data from a postgres DB
But any lines given after the psql.exe command does not works
after the psql.exe line the console asks for the password
and does nothing it's only when I press Ctrl+C the other lines get executed

I tried using Start-Job but then I am unable to read the output of my select command it only returns the following line
Job started: System.Management.Automation.PSRemotingJob stating that the job has started

I also tried the Invoke-Command but that too didn't help.

Can anyone help me with a simple sample that explains how to enter password for the psql.exe cmd and how to read the output from the select cmd

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2025-01-30 20:57:19

我正在分享对我有用的方法

$env:PGPASSWORD='password'
$result=Write-Output  "Select * FROM public.table_name" | & $psql -h 127.0.0.1 -p 5432 -U -U postgres -d database_name 

,现在您可以从结果变量中访问Select的输出。
您可以将方法用于方法并迭代结果以读取每一行。

I am sharing the approach that worked for me

$env:PGPASSWORD='password'
$result=Write-Output  "Select * FROM public.table_name" | & $psql -h 127.0.0.1 -p 5432 -U -U postgres -d database_name 

Now you can access the output of the select from the result variable.
You can use a for method and iterate over result to read each row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文