Skip to content

dbname connection parameter incorrectly sent as startup parameter in v1.11.0+ #1256

@yunliaw

Description

@yunliaw

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

  1. DSN parsed: o["database"] = "mydb"
  2. startup() has special handling for dbname → sends correct database field to server

v1.11.1

  1. DSN parsed: o["database"] = "mydb"
  2. setFromTag() loop looks for o["dbname"] (based on struct tag) - not found
  3. o["database"] remains in the map
  4. After loop: database → dbname conversion creates o["dbname"] = "mydb"
  5. o["dbname"] goes into cfg.Runtime (leftover keys)
  6. startup() sends cfg.Runtime parameters to server
  7. PostgreSQL rejects dbname as unknown startup parameter → error 42704

Fix

database -> dbname alias conversion should happen before the setFromTag

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions