简体   繁体   中英

postgres, psql and a bat file

I'm going around in circles and so need some help.

I want to be able to run an .sql against a database which is a scheduled task via a bat file. However when i run the bat file manually I get prompted for a password. I enter the password and then get told that....

"psql: FATAL: password authentication failed for user "(my windows login)"
'-h' is not recognised as an internal or external command, 
operable program or batch file. 

At the moment my bat reads...

@echo off
"D:\Program Files (x86)\PostgreSQL\9.1\bin\psql.exe"
-h localhost -U postgres -d database_name -f D:/scripts/SQL/test.sql
pause

First thing, what cmd do i need to add to populate the password request

What am I doing wrong with the rest of the statement to get it to load the .sql

Thanks

by adding this line to your config file ( pg_hba.conf ), you can tell postgres to allow local connections without authentication

local      <database>  <user>  trust

http://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html

All that needs to go into a single line in your batch file (it seems you have two lines):

@echo off
"D:\Program Files (x86)\PostgreSQL\9.1\bin\psql.exe" -h localhost -U postgres -d database_name -f D:/scripts/SQL/test.sql
pause

To avoid the password prompt, set the environment variable PGPASSWORD before calling psql:

@echo off
setlocal
set PGPASSWORD=my_very_secret_password
"D:\Program Files (x86)\PostgreSQL\9.1\bin\psql.exe" -h localhost -U postgres -d database_name -f D:/scripts/SQL/test.sql
pause
endlocal

The setlocal/endlocal commands are there to make sure the variable is cleared after the batch file was executed.

To avoid having the password in plain text in the batch file you can create a pgpass.conf file that contains the password. For details please see the manual: http://www.postgresql.org/docs/current/static/libpq-pgpass.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM