postgresql: .pgpass not working

瘦欲@ 提交于 2020-11-30 06:15:24

问题


I have created a .pgpass file in my home directory which looks like this

localhost:5432:somedb:someuser:somepass

I am using a shell script which creates a directory and puts a pg_dump of somedb there :

mkdir directory
pg_dump somedb > directory/somefile.dump

It still prompts for the password.

Where is the mistake here ?


回答1:


Did you try specifying the host, user, & db?

pg_dump -U someuser -h localhost somedb > directory/somefile.dump



回答2:


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:

/bin/chmod 0600 ~/.pgpass



回答3:


  1. Create .pgpass file with content

host:5432:somedb:someuser:somepass

  1. set the permissions

sudo chmod 600 .pgpass

  1. Set the file owner as the same user using which you logged in :

sudo chown login_username:login_username .pgpass

  1. Set PGPASSFILE environment variable :

export PGPASSFILE='/home/user/.pgpass'

Now check by connecting to database :

psql -h host -U someuser somedb

It did not prompt for a password, and logged-in to postgresql.




回答4:


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:

  1. Make sure the flags --password/-W and password= in the connection string are unset. Otherwise, the pgpass file will not be used.
  2. Make sure the environment variable PGPASSWORD is unset (echo $PGPASSWORD). Otherwise, the pgpass file will not be used.
  3. Make sure the pgpass file is in the right location ($PGPASSFILE or default ~/.pgpass or %APPDATA%\postgresql\pgpass.conf)
  4. Make sure the passfile is not readable, writable, or executable by group or other (e.g. chmod 600 ~/.pgpass); otherwise psql will print a warning.
  5. Make sure the passfile is a file (not a symlink); otherwise psql will print a warning.
  6. Make sure the passfile is readable by the psql user (e.g. cat ~/.pgpass); otherwise psql 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.
  7. 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.
  8. 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 the host name if non-empty, or the hostaddr ip address. Otherwise, the line will be ignored without warning.

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.




回答5:


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.



来源:https://stackoverflow.com/questions/16786011/postgresql-pgpass-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!