I have created a .pgpass
file in my home directory which looks like this
localhost:5432:somedb:someuser:somepass
I am using a shell scr
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
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:
--password
/-W
and password=
in the connection string are unset. Otherwise, the pgpass file will not be used.PGPASSWORD
is unset (echo $PGPASSWORD
). Otherwise, the pgpass file will not be used.$PGPASSFILE
or default ~/.pgpass
or %APPDATA%\postgresql\pgpass.conf
)chmod 600 ~/.pgpass
); otherwise psql
will print a warning.psql
will print a warning.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.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.*
. 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.
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
.
Did you try specifying the host, user, & db?
pg_dump -U someuser -h localhost somedb > directory/somefile.dump
host:5432:somedb:someuser:somepass
sudo chmod 600 .pgpass
sudo chown login_username:login_username .pgpass
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.