panic: dial tcp: lookup tcp/5432,: getaddrinfow: The specified class was not found

56 Views Asked by At

Can anyone help me with this error please?

panic: dial tcp: lookup tcp/5432,: getaddrinfow: The specified class was not found.

goroutine 1 [running]:
main.main()
        C:/Dev/Digitalent/scalable-web-service-with-golang/go-sql/main.go:35 +0x23a
exit status 2

this is the code.

package main

import (
    "database/sql"
    "fmt"

    _ "github.com/lib/pq"
)

const (
    host     = "localhost"
    port     = 5432
    username = "postgres"
    password = "*********"
    dbName   = "db-go-sql"
)

var (
    db  *sql.DB
    err error
)

func main() {
    psqlURL := fmt.Sprintf("host=%s port=%d, user=%s, password=%s "+
        "dbname=%s sslmode=disable", host, port, username, password, dbName)

    db, err = sql.Open("postgres", psqlURL)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err)
    }

    fmt.Println("Connection to postgresql established")
}

It's about connecting my Go Lang code with PostgreSQL. I have checked the connection over and over, such as host, port, username, password, and dbname, and it's all good I think. But, this error always came up when I run the main program. I'm using Windows 11. Thank you!

1

There are 1 best solutions below

0
PRATHEESH PC On
psqlURL := fmt.Sprintf("host=%s port=%d, user=%s, password=%s "+
        "dbname=%s sslmode=disable", host, port, username, password, dbName)

You do have a , with the url string. Removing that comma should resolve your problem.

psqlURL := fmt.Sprintf("host=%s port=%d user=%s password=%s "+
        "dbname=%s sslmode=disable", host, port, username, password, dbName)

This issue is originating from the lookupIP method in the lookup_windows.go file, when it look for a port as 5432,

var e error
for i := 0; i < dnsConf.attempts; i++ {
    e = syscall.GetAddrInfoW(name16p, nil, &hints, &result)
    if e == nil || e != _WSATRY_AGAIN || time.Since(start) > dnsConf.timeout {
        break
    }
}
if e != nil {
    err := winError("getaddrinfow", e)
    dnsError := &DNSError{Err: err.Error(), Name: name}
    if err == errNoSuchHost {
        dnsError.IsNotFound = true
    }
    return nil, dnsError
}