Postgresql:使用密码编写 psql 执行脚本
如何调用psql,使其不提示输入密码?
这就是我所拥有的:
psql -Umyuser < myscript.sql
但是,我找不到传递密码的参数,因此 psql 总是提示输入它。
How can I call psql so that it doesn't prompt for a password?
This is what I have:
psql -Umyuser < myscript.sql
However, I couldn't find the argument that passes the password, and so psql always prompts for it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
您可能希望阅读对 PostgreSQL 进行身份验证的方法的摘要。
要回答您的问题,有多种方法可以为基于密码的身份验证提供密码:
通过密码提示。示例:
在 pgpass 文件中。请参阅libpq-pgpass。格式:
使用
PGPASSWORD
环境变量。请参阅libpq-envars。示例:在连接字符串中 密码和其他选项可以在连接字符串/URI 中指定。请参阅 app-psql。示例:
You may wish to read a summary of the ways to authenticate to PostgreSQL.
To answer your question, there are several ways provide a password for password-based authentication:
Via the password prompt. Example:
In a pgpass file. See libpq-pgpass. Format:
With the
PGPASSWORD
environment variable. See libpq-envars. Example:In the connection string The password and other options may be specified in the connection string/URI. See app-psql. Example:
您可以在脚本的开头添加此命令行:
You can add this command line at the beginning of your script:
这可能是一个老问题,但是您可以使用一种没有人提到的替代方法。可以直接在连接 URI 中指定密码。该文档可以在此处找到,或者此处。
您可以直接在提供给
psql
的连接 URI 中提供您的用户名和密码:This might be an old question, but there's an alternate method you can use that no one has mentioned. It's possible to specify the password directly in the connection URI. The documentation can be found here, alternatively here.
You can provide your username and password directly in the connection URI provided to
psql
:如果您打算拥有多个主机/数据库连接,则 ~/.pgpass 文件 是要走的路。
步骤:
vim ~/.pgpass
或类似工具创建文件。按以下格式输入您的信息:主机名:端口:数据库:用户名:密码
不要在字段值周围添加字符串引号。您还可以使用 * 作为端口/数据库字段的通配符。chmod 0600 ~/.pgpass
,以便 psql 不会默默地忽略它。alias postygresy='psql --host hostname database_name -U username'
这些值应与您在 ~/.pgpass 文件中输入的值相匹配。获取您的 bash 配置文件。 ~/.bashrc
或类似的。请注意,如果您设置了导出 PGPASSWORD='' 变量,它将优先于该文件。
If you intend on having multiple hosts/database connections, the ~/.pgpass file is the way to go.
Steps:
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.chmod 0600 ~/.pgpass
in order for it to not be silently ignored by psql.alias postygresy='psql --host hostname database_name -U username'
The values should match those that you inputted to the ~/.pgpass file.. ~/.bashrc
or similar.Note that if you have an export PGPASSWORD='' variable set, it will take precedence over the file.
使用 PGPASSWORD 环境变量的替代方法是根据 文档
An alternative to using
PGPASSWORD
environment variable is to useconninfo
string according to the documentation您必须创建一个密码文件:请参阅 http://www.postgresql。 org/docs/9.0/interactive/libpq-pgpass.html 了解更多信息。
You have to create a password file: see http://www.postgresql.org/docs/9.0/interactive/libpq-pgpass.html for more info.
如果您像我一样在 Windows 上遇到问题(我使用的是 Windows 7 64 位)并且
set PGPASSWORD=[Password]
不起作用。然后,正如 Kavaklioglu 在其中一条评论中所说,
您需要将其保存在文件顶部,或者在任何使用之前保存,以便在调用之前对其进行设置。
当然可以在 Windows 上运行:)
If you're having problems on windows like me (I'm using Windows 7 64-bit) and
set PGPASSWORD=[Password]
did not work.Then, as Kavaklioglu said in one of the comments,
You will need to save this at the top of the file, or before any usage so its set before being called.
Certainly does work on windows :)
考虑到使用 PGPASSWORD 环境变量的安全问题,我认为最好的整体解决方案如下:
这里有几点需要注意。第 1 步是为了避免破坏可能存在的用户的 ~/.pgpass 文件。您还必须确保该文件的权限为 0600 或更低。
有些人建议利用 bash 来简化此操作,如下所示:
这使用 <() 语法来避免需要将数据写入实际文件。但它不起作用,因为 psql 检查正在使用的文件并会抛出如下错误:
Given the security concerns about using the PGPASSWORD environment variable, I think the best overall solution is as follows:
There are a couple points of note here. Step 1 is there to avoid mucking with the user's ~/.pgpass file that might exist. You also must make sure that the file has permissions 0600 or less.
Some have suggested leveraging bash to shortcut this as follows:
This uses the <() syntax to avoid needing to write the data to an actual file. But it doesn't work because psql checks what file is being used and will throw an error like this:
只需使用 PGPASSWORD 即可完成。我正在使用 psql 9.5.10。在您的情况下,解决方案是
PGPASSWORD=password psql -U myuser
PGPASSWORD=password psql -U myuser
PGPASSWORD=password psql -U myuser
PGPASSWORD=password psql -U myuser
myscript.sql
It can be done simply using PGPASSWORD. I am using psql 9.5.10. In your case the solution would be
PGPASSWORD=password psql -U myuser < myscript.sql
对于那些不熟悉 *nix shell 脚本的人来说,基于 mightybyte 的答案,这里有一个工作脚本:
第 2 行
/tmp/pgpasswd$$
中的双美元符号 ($$
) 将进程 ID 号附加到文件名,这样这个脚本就可以多次运行,甚至可以同时运行,而不会产生副作用。请注意第 4 行使用
chmod
命令 - 就像 mightybyte 描述的“不是普通文件”错误一样,还有一个“如果不这样做,权限”错误。在第 7 行,您不必使用
-h
myserver、-p
myport 或如果您使用默认值(localhost:5432)并且只有一个数据库用户,则-U
jdoe 标志。对于多个用户,(但默认连接)将该行更改为不要忘记使脚本可执行
更新:
我采纳了RichVel的建议并在将密码放入文件之前,使文件不可读。这弥补了一个小小的安全漏洞。
谢谢!
Building on mightybyte's answer for those who aren't comfortable with *nix shell scripting, here's a working script:
The double dollar sign (
$$
) in/tmp/pgpasswd$$
at line 2 appends the process ID number to the file name, so that this script can be run more than once, even simultaneously, without side effects.Note the use of the
chmod
command at line 4 - just like the "not a plain file" error that mightybyte described, there's also a "permissions" error if this is not done.At line 7, you won't have to use the
-h
myserver, the-p
myport, or-U
jdoe flag if you use the defaults (localhost : 5432) and only have one database user. For multiple users, (but the default connection) change that line toDon't forget to make the script executable with
UPDATE:
I took RichVel's advice and made the file unreadable before putting the password into it. That closes a slight security hole.
Thanks!
您可能会发现这很有用: Windows PSQL 命令行:有没有办法允许无密码登录?
You may find this useful: Windows PSQL command line: is there a way to allow for passwordless login?
8 年后...
在我的 mac 上,我必须在文件中放入一行
~/.pgpass
类似:另请参阅:
https://www.postgresql.org/docs/current/libpq-pgpass。 html
https://wiki.postgresql.org/wiki/Pgpass
8 years later...
On my mac, I had to put a line into the file
~/.pgpass
like:Also see:
https://www.postgresql.org/docs/current/libpq-pgpass.html
https://wiki.postgresql.org/wiki/Pgpass
这也适用于其他 postgresql cli,例如您可以在非交互模式下运行 pgbench。
This also works for other postgresql clis for example you can run pgbench in non-interactive mode.
在 Windows 上,结合使用 url 字符串和使用 psql 选项:
传递不带 -d 的命令不起作用,仅连接到数据库,但不执行脚本
On Windows worked the combination of url string and using psql options:
passing the the command without -d was not working, just connected to the database, but no execution of the script
我发现,即使您定义了 PGPASSWORD 变量,psql 也会显示密码提示,但是您可以为 psql 指定 -w 选项来省略密码提示。
I find, that psql show password prompt even you define PGPASSWORD variable, but you can specify -w option for psql to omit password prompt.
在命令中使用-w:
psql -h localhost -p 5432 -U 用户 -w
Use -w in the command:
psql -h localhost -p 5432 -U user -w