如何调用psql以使它不提示输入密码?

这是我所拥有的:

psql -Umyuser < myscript.sql

但是,我找不到传递密码的参数,所以psql总是提示输入这个参数。


当前回答

这也适用于其他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 &

其他回答

您可能希望阅读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

PGPASSWORD=[your password] psql -Umyuser < myscript.sql

我发现,即使你定义了PGPASSWORD变量,psql显示密码提示,但你可以指定-w选项为psql省略密码提示。

在命令中使用-w: psql -h localhost -p 5432 -U user -w

使用PGPASSWORD环境变量的替代方法是根据文档使用conninfo字符串

指定连接参数的另一种方法是在conninfo中 字符串或URI,用来代替数据库名称。这 机制使您可以对连接进行非常广泛的控制。

$ psql "host=<server> port=5432 dbname=<db> user=<user> password=<password>"

postgres=>