Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search the password in .pgpass is not working when using UNIX paths. #73

Open
lesovsky opened this issue May 29, 2021 · 1 comment
Open

Comments

@lesovsky
Copy link

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):

  1. Create .pgpass file
/var/run/postgresql/:5432:postgres:vasya:vasya123
  1. 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.

@jackc
Copy link
Owner

jackc commented May 29, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants