lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.04k stars 909 forks source link

Incorrect connection string does not error #1090

Closed rgarlik closed 2 years ago

rgarlik commented 2 years ago

Calling the sql.Open function returns an empty error object when the connection string is empty. Incorrect connection string should provide some sort of error even if sql.Open does not attempt a database connection by itself.

Example code:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/lib/pq"
)

func main() {
    _, err := sql.Open("postgres", "") // notice the empty connection string
    if err != nil {
        panic("Error!")
    }

    fmt.Println("Everything went great")
}

produces the following output:

Everything went great

Attempting to Ping() the created database will result in a not-very-helpful error message after attempting to connect to a default configuration:

dial tcp [::1]:5432: connectex: No connection could be made because the target machine actively refused it.

Tested on go1.19 windows/amd64

rgarlik commented 2 years ago

It seems a blank string is supposed to connect you to a local instance of Postgres and this is expected behavior, my mistake, I'll dig in a little deeper before opening an issue next time. Sorry for wasting your time.