-
-
Notifications
You must be signed in to change notification settings - Fork 941
Closed
Description
Description
We found this bug after attempting to upgrade lib/pq from v1.10.9 to v1.11.1. We got connections fail with:
pq: unrecognized configuration parameter "dbname" (42704)
This occurs because dbname is being included in the PostgreSQL startup message as a runtime parameter, rather than being used only client-side to populate the database field.
Reproduction
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
func main() {
// Using "database=" in DSN
db, err := sql.Open("postgres", "host=localhost user=postgres password=secret port=5432 database=mydb sslmode=disable")
if err != nil {
fmt.Println("Open error:", err)
return
}
err = db.Ping()
if err != nil {
fmt.Println("Ping error:", err) // pq: unrecognized configuration parameter "dbname" (42704)
}
}Root Cause
From the preliminary investigation it looks like that the regression is introduced in #1240. Here's the break down:
v1.10.9
- DSN parsed: o["database"] = "mydb"
- startup() has special handling for dbname → sends correct database field to server
v1.11.1
- DSN parsed: o["database"] = "mydb"
- setFromTag() loop looks for o["dbname"] (based on struct tag) - not found
- o["database"] remains in the map
- After loop: database → dbname conversion creates o["dbname"] = "mydb"
- o["dbname"] goes into cfg.Runtime (leftover keys)
- startup() sends cfg.Runtime parameters to server
- PostgreSQL rejects dbname as unknown startup parameter → error 42704
Fix
database -> dbname alias conversion should happen before the setFromTag
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels