pgconn
pgconn copied to clipboard
Search the password in .pgpass is not working when using UNIX paths.
Hi,
I use 'pgx' driver in one of my project, and one of the user has created an issue related to reading password from .pgpass is not working.
I tried to dig into the problem and found, that UNIX path specified in host parameter of connection settings, is not used for searching the password in pgpass. Instead of this the "localhost" string is used, hence this leads that password is not found.
Steps to reproduce (setting up hba is omitted here, it is supposed that psql is successfully connecting using the .pgpass):
- Create .pgpass file
/var/run/postgresql/:5432:postgres:vasya:vasya123
- Use the following code to test connection.
func Test_Example(t *testing.T) {
// Make sure that passfile is parsed successfully
passfile, err := pgpassfile.ReadPassfile("/home/lesovsky/.pgpass")
assert.NoError(t, err)
for _, e := range passfile.Entries {
fmt.Println(e)
}
cfg, err := pgx.ParseConfig("host=/var/run/postgresql/ port=5432 user=vasya dbname=postgres")
assert.NoError(t, err)
// password is empty, but must be "vasya123".
assert.Empty(t, cfg.Password)}
After ParseConfig has been done, the Password is empty because "localhost" string is used for searching the password.
It is not critical for me and I have a workaround (use localhost instead of paths in .pgpass), but could you explain reasons of this behavior.
Not sure. To be honest I don't think I ever use a password for a Unix connection.
It does seem odd to force the host to localhost instead of using the path. But I think it was a (misguided?) attempt to follow this behavior (https://www.postgresql.org/docs/current/libpq-pgpass.html):
The host name localhost is also searched for when the connection is a Unix-domain socket connection and the host parameter matches libpq's default socket directory path.
Maybe instead it should try config.Host and if nothing is found and the network is unix then try localhost.