postgresqlpowershellscriptingwindows-scripting

a powershell script to read data from a postgres table


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


Solution

  • 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.