如何调用psql以使它不提示输入密码?
这是我所拥有的:
psql -Umyuser < myscript.sql
但是,我找不到传递密码的参数,所以psql总是提示输入这个参数。
如何调用psql以使它不提示输入密码?
这是我所拥有的:
psql -Umyuser < myscript.sql
但是,我找不到传递密码的参数,所以psql总是提示输入这个参数。
当前回答
8年后……
在我的mac上,我必须在文件中添加一行 ~ /。pgpass:
<IP>:<PORT>:<dbname>:<user>:<password>
还看到: https://www.postgresql.org/docs/current/libpq-pgpass.html https://wiki.postgresql.org/wiki/Pgpass
其他回答
这也适用于其他postgresql cliis,例如你可以在非交互模式下运行pgbench。
export PGPASSWORD=yourpassword
/usr/pgsql-9.5/bin/pgbench -h $REMOTE_PG_HOST -p 5432 -U postgres -c 12 -j 4 -t 10000 example > pgbench.out 2>&1 &
8年后……
在我的mac上,我必须在文件中添加一行 ~ /。pgpass:
<IP>:<PORT>:<dbname>:<user>:<password>
还看到: https://www.postgresql.org/docs/current/libpq-pgpass.html https://wiki.postgresql.org/wiki/Pgpass
如果你像我一样在windows上遇到问题(我使用的是windows 7 64位),设置PGPASSWORD=[Password]不工作。
然后,正如Kavaklioglu在其中一条评论中所说,
export PGPASSWORD=[password]
您需要将其保存在文件的顶部,或者在任何使用之前,以便在调用之前设置它。
当然可以在windows上工作:)
如果您打算拥有多个主机/数据库连接,则~/。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= "变量,它将优先于文件。
使用PGPASSWORD环境变量的替代方法是根据文档使用conninfo字符串
指定连接参数的另一种方法是在conninfo中 字符串或URI,用来代替数据库名称。这 机制使您可以对连接进行非常广泛的控制。
$ psql "host=<server> port=5432 dbname=<db> user=<user> password=<password>"
postgres=>