Nathan Takemori
Nathan Takemori

Reputation: 139

Data types between PostgreSQL and Golang

type User struct {
    Email     string `json:"email"`
    Password  string `json:"password"`
}

db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("email: ", reflect.TypeOf(usr.Email)) //string
    fmt.Println("salt: ", reflect.TypeOf(salt))       //[]uint8
    fmt.Println("hash: ", reflect.TypeOf(hash))       //string

    sql := `INSERT INTO public."Users" (email, password, salt) VALUES ($1, $2, $3)`

    _, err = db.Exec(sql, usr.Email, hash, salt)

throws error: "pq: invalid byte sequence for encoding "UTF8": 0x97"
my table: "email" type: TEXT, "password" type: TEXT, "salt" type: smallint[] (thinking this might be the cause of the error but I am not sure what to use instead)

Upvotes: 1

Views: 1464

Answers (1)

Nathan Takemori
Nathan Takemori

Reputation: 139

PostgreSQL bytea = []unit8 Golang

changed type and issue was resolved!

Upvotes: 1

Related Questions