Reputation: 7278
I have installed my Postgresql database on a Windows server environment. I'd like to schedule a job using Windows Task scheduler to run every night so I need to run the following command without asking for password:
psql -U myUserName-d myDBName -c "select MyFunctionName()"
When I run the above query in my cmd
shell, it asks me for password. When I enter the password manually, the function is correctly run.
So my solution is to read from the pgpass.conf
file so no password is required.
Here are the things I have done to achieve this:
I created the pgpass.conf
file in a directory I created in the %appdata%
(AppData\Roaming\postgresql
to be precise).
Here are the contents of this file:
localhost:5432:myDBName:myUserName:myPassword
I have also tried with the value 127.0.0.1
instead of localhost
above.
I, then, added the an environment variable (in the user variables for administrator list) called PGPASSFILE
and gave it the pgpass.conf
location.
;C:\Users\administrator\AppData\Roaming\postgresql\pgpass.conf
Finally I stopped and restarted my Postgres service on Windows services and re-ran the command. But it is still asking for password.
How can I let my command know from where to read the password?
Upvotes: 4
Views: 4320
Reputation: 246483
If you don't want to set the PGPASSFILE
environment variable, put the password file in the standard location %APPDATA%\postgresql\pgpass.conf
as described by the documentation.
Upvotes: 1