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?
--%if you want$userto expand before passing the argument topsql--%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.