locked
When running psql with powershell sits and asks for password RRS feed

  • Question

  • Hi There,

    Does anyone knows how to fix this here's my example code.

    It just waits for password when I run the powershell script.

    ps1 script content:

    $DBPort='5659'
    $DBName='postgres'
    $DBUser='User1'
    $psqlEx='f:\pgAdmin 4\v4\v4\psql.exe'
    $PGPASSWORD='Q%MYVdoX9^9iJ2QrNs$$-!]'
    [string] $DBHost='localhost'
    $DBSql="select usename from pg_user "

    & .\psql.exe -h $DBHost "-U $DBUser" "-p $DBPort" "-c $DBSql" > .\output.csv

     Also I've noticed that the password is not passed correctly because of the special characters so I guess those needs to be escaped somehow.

    Thank you

    Wednesday, March 25, 2020 7:55 PM

All replies

  • Please ask PostGres questions in a PostGres forum.  

    .\psql.exe -h $DBHost -U $DBUser -w -p $DBPort -c $DBSql 


    \_(ツ)_/


    • Edited by jrv Friday, March 27, 2020 12:09 AM
    Wednesday, March 25, 2020 8:38 PM
  • Thanks for the replay..

    I tough it was a Powershell question.

    -W doesn't work, still promts for password.

     

    Thursday, March 26, 2020 2:25 PM
  • Thanks for the replay..

    I tough it was a Powershell question.

    -W doesn't work, still promts for password.

     

    You need to post your issue in a PostGres forum.   

    \_(ツ)_/

    Thursday, March 26, 2020 3:13 PM
  • No mention at the URL below of a "-W" flag, but I didn't go looking any further.

    Consider:

    https://stackoverflow.com/questions/6405127/how-do-i-specify-a-password-to-psql-non-interactively/6405162

    PS You probably can't declare a PowerShell variable and expect a Win32 app to be able to fetch it.

    Thursday, March 26, 2020 4:30 PM
  • -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.
    -W
    --password	Force psql to prompt for a password before connecting to a database.

    https://w3resource.com/PostgreSQL/connect-to-postgresql-database.php

    YOU must ask psql questions in a PostGres forum.  To avoid prompts for a  password you must configure your system correctly.


    \_(ツ)_/

    Friday, March 27, 2020 12:09 AM
  • No mention at the URL below of a "-W" flag, but I didn't go looking any further.

    Consider:

    https://stackoverflow.com/questions/6405127/how-do-i-specify-a-password-to-psql-non-interactively/6405162

    PS You probably can't declare a PowerShell variable and expect a Win32 app to be able to fetch it.

    I fixed my original post.  The "W" must be lowercase to force a stop the password prompt


    \_(ツ)_/

    Friday, March 27, 2020 12:11 AM