Thursday, April 25, 2024
HomeGolangCan't "Scan" datetime from PostgreSQL - Technical Dialogue

Can’t “Scan” datetime from PostgreSQL – Technical Dialogue


I wish to use a PostgreSQL database from a Go webserver, and as a primary step I simply need fundamental entry to the database. I can create a desk, and add a file, however on retrieval f stated file, Go chokes on the timestamp.

2023/05/24 14:15:36 sql: Scan error on column index 3, identify “created_at”: unsupported Scan, storing driver.Worth kind into kind *time.Time

That is the road it’s failing at.

        if err := db.QueryRow(question, 1).Scan(&id, &username, &password, &createdAt); err != nil {

I’ve discovered point out of a difficulty with MySQL that entails setting one thing within the connection string ( parseTime=true), however nothing for Postgres. I’m solely simply beginning out with Go, and most of my code is copied from elsewhere – and that makes use of MySQL.

bundle major

import (
    "database/sql"
    "fmt"
    "log"
    "time"

    _ "github.com/lib/pq"
)

const (
  host     = "localhost"
  port     = 5432
  person     = "postgres"
  password = "*******"
  dbname   = "com_dev"
)


func major() {

    psqlInfo := fmt.Sprintf("host=%s port=%d person=%s "+
      "password=%s dbname=%s sslmode=disable",
      host, port, person, password, dbname)
    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        log.Deadly(err)
    }
    if err := db.Ping(); err != nil {
        log.Deadly(err)
    }

    { // Create a brand new desk
        fmt.Println("Attempting to create person desk")
        question := `
            CREATE TABLE customers (
                id SERIAL PRIMARY KEY,
                username TEXT NOT NULL,
                password TEXT NOT NULL,
                created_at TIMESTAMP
            );`

        if _, err := db.Exec(question); err != nil {
            fmt.Println(err) 
            //log.Deadly(err)
        }
    }

    { // Insert a brand new person
        fmt.Println("Attempting so as to add person")
        username := "johndoe"
        password := "secret"
        createdAt := time.Now()

        outcome, err := db.Exec("INSERT INTO customers (username, password, created_at) VALUES ($1, $2, $3)", username, password, createdAt)
        if err != nil {
            fmt.Println("add failed")
            log.Deadly(err)
        }

        id, err := outcome.LastInsertId()
        fmt.Println(id)
    }

    { // Question a single person
        fmt.Println("Looking for person")
        var (
            id        int
            username  string
            password  string
            createdAt time.Time
        )

        question := "SELECT id, username, password, created_at FROM customers WHERE id = $1"
        if err := db.QueryRow(question, 1).Scan(&id, &username, &password, &createdAt); err != nil {
            log.Deadly(err)
        }

        fmt.Println(id, username, password, createdAt)
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments