0

I am attempting to use PSQL in Powershell to set up users and associated passwords. I have all the user accounts set up, but I'm running into trouble doing the following:

$env:PGPASSWORD = "superUserPassword"
$USER_PASSWORD = "databaseUserPassword"

function set_password 
{
    [CmdletBinding()]
    param 
    (
        [string]$user,
        [string]$password
    )
    psql --% -h x.x.x.x -U admin@instance -d postgres -c "ALTER USER $user WITH PASSWORD '$password';"

}
set_password testUser $USER_PASSWORD

This code ends in an error that doesn't make a lot of sense:

psql : ERROR:  syntax error at or near "$user"
At line:12 char:2
+     psql --% -h 10.77.227.166 -U dhadmin@dh-dataint-psql1 -d postgres ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (ERROR:  syntax ... near "'$user'":String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
LINE 1: ALTER USER $user WITH PASSWORD '$password';
                   ^

If I use the actual username, rather than calling $user, it works just fine. I've tried using '$user', similar to password, but that didn't work either, and generates the same error as above, just with '$user' instead. What am I doing wrong?

2
  • 1
    You're gonna have to remove --% if you want $user to expand before passing the argument to psql Commented Jun 10, 2021 at 15:49
  • Oh huh.. i had added the --% per the advice of this thread to get the psql commands working in the first place. But you're right, after removing it, the command will run. I guess I hadn't been supplying arguments in that fashion in my previous testing, so the --% made sense. Commented Jun 10, 2021 at 15:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.