Search code examples
windowspostgresqlpowershelldatabase-backupsqliksense

How to use PGPASS file in Powershell to avoid password prompt?


I had to automate my postgre database backup. As instructed by my software vendor I am trying to use pg_dump.exe (see below) file to take a backup but that prompts me for password.

.\pg_dump.exe -h localhost -p 4432 -U postgres -v -b -F t -f "C:\Backup\Backup.tar" Repo

So googled and found that as per "https://www.postgresql.org/docs/9.6/libpq-pgpass.html" I can create a pgpass.conf file within 'C:\Users\User1\AppData\Roaming\postgresql\pgpass.conf" which I did.

Then I tried to pass data of pgpass.conf file to env variable before executing my pg_dump command. But it is not working. Still I am getting prompt to enter password. This is the content of pgpass.conf file: *:*:*:postgres:password

Below is the code I am trying in PowerShell,

   $Env:PGPASSFILE="C:\Users\User1\AppData\Roaming\postgresql\pgpass.conf"
   cd "C:\Program Files\Qlik\Sense\Repository\PostgreSQL\9.6\bin"
   .\pg_dump.exe -h localhost -p 4432 -U postgres -v -b -F t -f "C:\Backup\Backup.tar" Repo

Why am I still being asked for password?

When I type following code $Env:AppData I get following response "C:\Users\User1\AppData\Roaming"

Everywhere there are guidance on how to use it in UNIX or command prompt but not in powershell. Any help is appreciated. Also if you could direct me how to secure this password file then it will be great.

With password prompt I cannot automate it with windows task scheduler.


Solution

  • I used pg_hba file to allow connection "trust" (riskier method but I had to get things done ASAP).