bash postgresql:.pgpass 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16786011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
postgresql: .pgpass not working
提问by Deepankar Bajpeyi
I have created a .pgpass
file in my home directory which looks like this
我.pgpass
在我的主目录中创建了一个文件,看起来像这样
localhost:5432:somedb:someuser:somepass
localhost:5432:somedb:someuser:somepass
I am using a shell script which creates a directory and puts a pg_dump of somedb
there :
我正在使用一个 shell 脚本,它创建一个目录并在somedb
那里放置一个 pg_dump :
mkdir directory
pg_dump somedb > directory/somefile.dump
It still prompts for the password.
它仍然提示输入密码。
Where is the mistake here ?
这里的错误在哪里?
采纳答案by devnull
Did you try specifying the host & user?
您是否尝试指定主机和用户?
pg_dump -U someuser -h localhost somedb > directory/somefile.dump
回答by Nacho Mezzadra
Although question has already been answered and accepted, it may also happen that permissions on .pgpass file are not properly set. It has to have the world and group access disallowed:
虽然问题已经得到回答和接受,但也可能会发生 .pgpass 文件的权限设置不正确的情况。它必须禁止世界和组访问:
/bin/chmod 0600 ~/.pgpass
回答by Shrinivas
1) Create .pgpass file with content
1) 用内容创建 .pgpass 文件
host:5432:somedb:someuser:somepass
主机:5432:somedb:someuser:somepass
2) set the permissions using command
2)使用命令设置权限
sudo chmod 600 .pgpass
须藤 chmod 600 .pgpass
3) Set the file owner as the same user using which you logged in :
3) 将文件所有者设置为您登录时使用的同一用户:
sudo chown login_username:login_username .pgpass
须藤 chown login_username:login_username .pgpass
4) Set PGPASSFILE environment variable :
4) 设置 PGPASSFILE 环境变量:
export PGPASSFILE='/home/user/.pgpass'
导出 PGPASSFILE='/home/user/.pgpass'
Now check by connecting to database :
现在通过连接到数据库来检查:
psql -h host -U someuser somedb
psql -h host -U someuser somedb
It will not prompt for password and logged in to postgresql.
它不会提示输入密码并登录到 postgresql。
回答by yonran
psql
(startup.c) calls PQconnectdbParams
(fe-connect.c), and then passwordFromFile
is called. Here's a checklist to make sure the pgpass file will be used:
psql
( startup.c) 调用PQconnectdbParams
( fe-connect.c),然后passwordFromFile
被调用。这是确保将使用 pgpass 文件的清单:
- Make sure the flags
--password
/-W
andpassword=
in the connection stringare unset. Otherwise, the pgpass file will not be used. - Make sure the environment variable
PGPASSWORD
is unset (echo $PGPASSWORD
). Otherwise, the pgpass file will not be used. - Make sure the pgpass fileis in the right location (
$PGPASSFILE
or default~/.pgpass
or%APPDATA%\postgresql\pgpass.conf
) - Make sure the passfile is not readable, writable, or executable by group or other (e.g.
chmod 600 ~/.pgpass
); otherwisepsql
will print a warning. - Make sure the passfile is a file (not a symlink); otherwise
psql
will print a warning. - Make sure the passfile is readable by the
psql
user (e.g.cat ~/.pgpass
); otherwisepsql
will ignore it without warning. Make sure that it is owned by the user, and that all its ancestor directories are executable by the user. - Make sure that the pgpass file has the correct format
hostname:port:database:username:password
(The Password File,passwordFromFile
). Each field (other than password) can be*
. The characters:
and\
must be escaped\:
and\\
(even in password). The password ends at:
or the end of the line and can include any byte other than\r
,\n
, or\0
. Any lines that aren't formatted right or don't match the host and user are ignored without warning as if they were comments. - Make sure the hostname, port, dbname, username of the line in the pgpass file match the server or are
*
. The server's “pwhost” that is checked is thehost
name if non-empty, or thehostaddr
ip address. Otherwise, the line will be ignored without warning.
- 确保标志
--password
/-W
和password=
中连接字符串都没有设置。否则,将不会使用 pgpass 文件。 - 确保环境变量
PGPASSWORD
未设置 (echo $PGPASSWORD
)。否则,将不会使用 pgpass 文件。 - 确保pgpass 文件位于正确的位置(
$PGPASSFILE
或默认~/.pgpass
或%APPDATA%\postgresql\pgpass.conf
) - 确保密码文件不可被组或其他人读取、写入或执行(例如
chmod 600 ~/.pgpass
);否则psql
会打印警告。 - 确保密码文件是一个文件(不是符号链接);否则
psql
会打印警告。 - 确保
psql
用户可以读取密码文件(例如cat ~/.pgpass
);否则psql
将在没有警告的情况下忽略它。确保它归用户所有,并且它的所有祖先目录都可由用户执行。 - 确保 pgpass 文件具有正确的格式
hostname:port:database:username:password
(密码文件,passwordFromFile
)。每个字段(密码除外)都可以是*
. 字符:
和\
必须转义\:
和\\
(即使在密码中)。密码在行尾:
或行尾结束,可以包含除\r
、\n
、 或之外的任何字节\0
。任何格式不正确或与主机和用户不匹配的行都将被忽略而不会发出警告,就好像它们是注释一样。 - 确保 pgpass 文件中该行的主机名、端口、数据库名、用户名与服务器匹配或为
*
. 被检查的服务器的“pwhost”是host
名称(如果非空)或hostaddr
IP地址。否则,该行将在没有警告的情况下被忽略。
Unfortunately, there is no logging within these files, so if this doesn't help, then you may need to compile psql
and libpq
yourself and run it in a debugger.
不幸的是,这些文件中没有日志记录,所以如果这没有帮助,那么您可能需要自己编译psql
并libpq
在调试器中运行它。
回答by Jonathan dos Santos
Check the owner of the .pgpass file. Just lost half an hour to find out I had created my .pgpass file with sudo. The command for my user and location was chown postgres:postgres /var/lib/pgsql/.pgpass
.
检查 .pgpass 文件的所有者。花了半个小时才发现我用 sudo 创建了我的 .pgpass 文件。我的用户和位置的命令是chown postgres:postgres /var/lib/pgsql/.pgpass
.