How do I solve «panic: sql: unknown driver “postgres” (forgotten import?)»?

You imported the sql/database, a package contains generic interface for sql-related operation.

Since it’s only generic interface, you need to import the concrete implementation of the interface, and in this context, it’s the database driver.

From your code: sql.Open("postgres", psqlInfo), I presume you are using postgresql database. There are some postgresql drivers for golang available, one of them is https://github.com/lib/pq driver. So add it to the import statement.

package main

import (
    "bufio"
    "database/sql"
    "encoding/csv"
    "encoding/json"
    "fmt"
    "io"
    "log"
    "os"
    _ "github.com/lib/pq" // <------------ here
)

The database driver pq must be imported with _ character in front of the import statement. It’s because we don’t use it explicitly on the code meanwhile it’s still required by database/sql package. For more details see this related SO question What does an underscore in front of an import statement mean?.

More information about golang sql: https://pkg.go.dev/database/sql.
.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)