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

Param overriding doesn't work for some fields #2003

Closed
cat1s-smile opened this issue May 6, 2024 · 1 comment
Closed

Param overriding doesn't work for some fields #2003

cat1s-smile opened this issue May 6, 2024 · 1 comment
Labels

Comments

@cat1s-smile
Copy link

Describe the bug
I'm trying to create Config by parsing empty conn string and getting auth error
failed to connect to 'host=localhost user=test database=test_namespace': failed SASL auth (FATAL: password authentication failed for user "test" (SQLSTATE 28P01))

To Reproduce
Steps to reproduce the behavior:
Create Config by parsing empty conn string, fill required params and try to connect to DB

If possible, please provide runnable example such as:

package main

import (
	"context"

	"github.com/jackc/pgx/v5"
	"github.com/jackc/pgx/v5/pgxpool"
)

func main() {
	cfg, _ := pgxpool.ParseConfig("")
	cfg.ConnConfig.Host = "localhost"
	cfg.ConnConfig.Port = 5444
	cfg.ConnConfig.Database = "test_namespace"
	cfg.ConnConfig.User = "test"
	cfg.ConnConfig.Password = "test"
	pgp, err := pgxpool.NewWithConfig(context.Background(), cfg)
	if err != nil {
		log.Fatal(err)
	}
	rs, err := pgp.Query(context.Background(), "select 1")
	fmt.Println(err)
	fmt.Println(rs.Next())
	fmt.Println(rs.Values())
}

However if I change exactly one line with config parsing
cfg, _ := pgxpool.ParseConfig("port=5444 database=test_namespace")
everything works properly

Expected behavior
successful run after soecifying all the params as cfg.ConnConfig.ParamName = ...

Actual behavior
failed to connect to 'host=localhost user=test database=test_namespace': failed SASL auth (FATAL: password authentication failed for user "test" (SQLSTATE 28P01))

Version

  • Go: go1.22.1 windows/amd64
  • PostgreSQL: PostgreSQL 12.2 (Debian 12.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
  • pgx: v5.5.5

Additional context
Add any other context about the problem here.

@cat1s-smile cat1s-smile added the bug label May 6, 2024
@jackc
Copy link
Owner

jackc commented May 10, 2024

From the docs for pgconn.ParseConfig:

The returned *Config may be modified. However, it is strongly recommended that any configuration that can be done through the connection string be done there. In particular the fields Host, Port, TLSConfig, and Fallbacks can be interdependent (e.g. TLSConfig needs knowledge of the host to validate the server certificate). These fields should not be modified individually. They should all be modified or all left unchanged.

Some types of connection attempts such as multiple hosts (for HA) and the default PGSSLMODE=prefer are implemented via "fallback configs". A fallback config is a different Host, Port, and TLSConfig. This means it tries the connection with first with the base config and then it tries each fallback config and uses its Host, Port, and TLSConfig.

Presumably, the connection config that actually works is one of the fallback configs and that is not changed by updating the top-level Config.Port.

@jackc jackc closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants