Home > Enterprise >  connecting postgresql to go
connecting postgresql to go

Time:01-02

package main

import (
    "database/sql"
    "fmt"
    "html/template"
    "net/http"
    "unicode"

    _ "github.com/lib/pq"
    "golang.org/x/crypto/bcrypt"
)

/* const (
    host = "localhost"
    port = 5432
    user = "postgres"
    password = "*******"
    dbname = "db"
) */

var tpl *template.Template
var db *sql.DB

func main() {
    tpl, _ = template.ParseGlob("templates/*.html")
    var err error
    db, err = sql.Open("postgresql", "root:password@tcp(localhost:localhost/db")
    if err != nil {
        panic(err.Error())
    }
    defer db.Close()
    http.HandleFunc("/register", registerHandler)
    http.HandleFunc("/registerauth", registerAuthHandler)
    fmt.Println("Listening")
    http.ListenAndServe("localhost:8080", nil)
}

Blockquote

when I run this i get an error = panic: sql: unknown driver "postgresql" (forgotten import?) btw, i'm just following a lesson online but they are using mysql while i on the other uses postgres and i'm doing this for my thesis

CodePudding user response:

You should use the "postgres" database driver string, not "postgresql".

  • Related