简体   繁体   English

一个 powershell 脚本,用于从 postgres 表中读取数据

[英]a powershell script to read data from a postgres table

I am creating a powershell script to read data from a postgres DB我正在创建一个 powershell 脚本来从 postgres 数据库中读取数据
But any lines given after the psql.exe command does not works after the psql.exe line the console asks for the password但是在psql.exe命令之后给出的任何行在控制台要求输入密码的psql.exe行之后不起作用
and does nothing it's only when I press Ctrl+C the other lines get executed什么都不做,只有当我按下Ctrl+C时,其他行才会执行

I tried using Start-Job but then I am unable to read the output of my select command it only returns the following line我尝试使用 Start-Job 但后来我无法读取 select 命令的 output 它只返回以下行
Job started: System.Management.Automation.PSRemotingJob stating that the job has started Job started: System.Management.Automation.PSRemotingJob说明作业已启动

I also tried the Invoke-Command but that too didn't help.我也尝试了 Invoke-Command 但这也没有帮助。

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谁能帮我做一个简单的示例,解释如何为 psql.exe cmd 输入密码以及如何从 select cmd 读取 output

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.现在您可以从结果变量访问 select 的 output。 You can use a for method and iterate over result to read each row.您可以使用 for 方法并遍历结果来读取每一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM