如何调用psql以使它不提示输入密码?
这是我所拥有的:
psql -Umyuser < myscript.sql
但是,我找不到传递密码的参数,所以psql总是提示输入这个参数。
如何调用psql以使它不提示输入密码?
这是我所拥有的:
psql -Umyuser < myscript.sql
但是,我找不到传递密码的参数,所以psql总是提示输入这个参数。
当前回答
你可以在脚本的开头添加这个命令行:
export PGPASSWORD="[your password]"
其他回答
你必须创建一个密码文件:更多信息请参见http://www.postgresql.org/docs/9.0/interactive/libpq-pgpass.html。
你可能会发现这很有用:Windows PSQL命令行:是否有一种方法允许无密码登录?
在Windows上工作的url字符串和使用psql选项的组合:
> psql -d postgresql://username:password@localhost:5432/mydb -f myscript.sql
传递没有-d的命令没有工作,只是连接到数据库,但没有执行脚本
您可能希望阅读PostgreSQL身份验证方法的摘要。
为了回答你的问题,有几种方法为基于密码的身份验证提供密码:
Via the password prompt. Example: psql -h uta.biocommons.org -U foo Password for user foo: In a pgpass file. See libpq-pgpass. Format: <host>:<port>:<database>:<user>:<password> With the PGPASSWORD environment variable. See libpq-envars. Example: export PGPASSWORD=yourpass psql ... # Or in one line for this invocation only: PGPASSWORD=yourpass psql ... In the connection string The password and other options may be specified in the connection string/URI. See app-psql. Example: psql postgresql://username:password@dbmaster:5433/mydb?sslmode=require
这可能是一个老问题了,但是你可以使用一个没有人提到过的替代方法。可以在连接URI中直接指定密码。文档可以在这里找到,也可以在这里。
你可以在提供给psql的连接URI中直接提供你的用户名和密码:
# postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]
psql postgresql://username:password@localhost:5432/mydb