Thursday 2 October 2014

Logging into postgresql database through powershell

Something I spend a while trying to figure out that may be of help to someone is how to provide a password when logging into a database using psql.exe.

When logging in, powershell just hung because when logging in at the command line, you are then prompted to enter the user's password.

The way around this is to set a temporary environment variable for the username you are logging in as in the powershell script. This will only exist for the duration of the running script.

$Env:PGPASSWORD = "YourPasswordHere"
$sqlScript = "C:\script_location\script.sql"
$executeCommand = 'psql.exe -d DBNAME -U USERNAME -f $sqlScript'
iex "& $executeCommand"

No comments: