Monday, December 11, 2023
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 (

    _ ""

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 {
    if err := db.Ping(); err != nil {

    { // 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 {

    { // 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")

        id, err := outcome.LastInsertId()

    { // 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 {

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



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments