如何调用psql以使它不提示输入密码?
这是我所拥有的:
psql -Umyuser < myscript.sql
但是,我找不到传递密码的参数,所以psql总是提示输入这个参数。
如何调用psql以使它不提示输入密码?
这是我所拥有的:
psql -Umyuser < myscript.sql
但是,我找不到传递密码的参数,所以psql总是提示输入这个参数。
当前回答
在命令中使用-w: psql -h localhost -p 5432 -U user -w
其他回答
你必须创建一个密码文件:更多信息请参见http://www.postgresql.org/docs/9.0/interactive/libpq-pgpass.html。
只需使用PGPASSWORD即可完成。我使用的是psql 9.5.10。对你来说,解决办法是
PGPASSWORD= psql -U myuser密码< myscript.sql密码
在命令中使用-w: psql -h localhost -p 5432 -U user -w
您可能希望阅读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
如果您打算拥有多个主机/数据库连接,则~/。Pgpass文件是可行的。
步骤:
Create the file using vim ~/.pgpass or similar. Input your information in the following format: hostname:port:database:username:password Do not add string quotes around your field values. You can also use * as a wildcard for your port/database fields. You must chmod 0600 ~/.pgpass in order for it to not be silently ignored by psql. Create an alias in your bash profile that runs your psql command for you. For example:alias postygresy='psql --host hostname database_name -U username' The values should match those that you inputted to the ~/.pgpass file. Source your bash profile with . ~/.bashrc or similar. Type your alias from the command line.
注意,如果设置了export PGPASSWORD= "变量,它将优先于文件。