jackc / pgx

PostgreSQL driver and toolkit for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.Connect parser doesn't seem to match standard connection url

sai-sy opened this issue · comments

commented

Describe the bug
I am trying to test a connection between my go app and my DB. The example in the readme is

	// urlExample := "postgres://username:password@localhost:5432/database_name"
	conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))

Just for this test code I am hard coding the values in as such

// urlExample := "postgres://username:password@localhost:5432/database_name"
conn, err := pgx.Connect(context.Background(), "postgres://postgres.blahblah:[YOUR-PASSWORD]@aws-blahblah:5432/postgres")

I get an error:

Unable to connect to database: failed to connect to `host=postgres.blahblah user=sai database=`: hostname resolving error (lookup postgres.blahblah on ipaddrssshere
3: no such host)

It seems as though the connection string is being parsed as so the user is being read as the host. Not entirely sure where it gets "sai" from but it is from somewhere. No where do I use my name, so maybe I'm not understanding where the function is trying t pull these values

Not sure if I'm doing something wrong, just trying to connect my db.

Version

  • Go: $ go version -> go version go1.22.1 linux/amd64
commented

I can get around it by buliding a dsn through url.URL but I'm not sure as to why that would make a difference given that it should've been the same string at the end of the day. I should've done some more testing to figure that part out but I was frustrated and looking for a fix and found on using a DSN.

pgx uses url.Parse from the standard library internally, so this is not an error of pgx. What I guess is that in your real connection string you are using a reserved character in your username or password without escaping them, while building through url.URL properly escapes them. "sai" is showing up because this your current username.

commented

Thanks yea this could be it! I'm using a loaner computer as I'm in between building a new one so I haven't bothered downloading my nvim config with all the syntactic sugar it would come with to highlight that.