postgresql postgres、psql 和一个 bat 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11016730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:00:19  来源:igfitidea点击:

postgres, psql and a bat file

postgresqlbatch-file

提问by geomiles

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....

我希望能够通过 bat 文件针对作为计划任务的数据库运行 .sql。但是,当我手动运行 bat 文件时,会提示我输入密码。我输入密码,然后被告知....

"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.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

首先,我需要添加什么 cmd 来填充密码请求

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

我在语句的其余部分做错了什么以使其加载 .sql

Thanks

谢谢

回答by bpgergo

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

通过将此行添加到您的配置文件 ( pg_hba.conf),您可以告诉 postgres 允许无需身份验证的本地连接

local      <database>  <user>  trust

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

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

回答by a_horse_with_no_name

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.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:

为避免密码提示,请在调用 psql 之前设置环境变量 PGPASSWORD:

@echo off
setlocal
set PGPASSWORD=my_very_secret_password
"D:\Program Files (x86)\PostgreSQL.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.

setlocal/endlocal 命令用于确保在执行批处理文件后清除变量。

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

为了避免在批处理文件中使用纯文本密码,您可以创建一个包含密码的 pgpass.conf 文件。详情请看手册:http: //www.postgresql.org/docs/current/static/libpq-pgpass.html