Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

480
Views
What datatype would I use to store a sha265 password in a Postgres database

Solved, thank you for those asking me what error it was:

pq: invalid byte sequence for encoding "UTF8": 0xe3 0xb0 0xc4

This meant I needed to store the password as bytea

If I want to insert a sha265 password in a Postgres database what datatype would I use. Text does not seem to be working for me.

Here is the line of code with the query

query := fmt.Sprintf("INSERT INTO users(username,password) VALUES('%s','%s') returning uid;", username, hashedPassword)

For those wondering here is the Go code. Like I said though, it works fine as long as the password isn't encrypted so I don't think this will be of much help

func hashPassword(password string) string {
    h := sha256.New()
    passwordBytes := []byte(password)
    passwordHashed := h.Sum(passwordBytes)
    return string(passwordHashed)
}

func createUser(username string, password string, passwordConfirm string) bool {
    dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
    DB_USER, DB_PASSWORD, DB_NAME)
    db, err := sql.Open("postgres", dbinfo)
    checkErr(err)
    defer db.Close()

    hashedPassword := hashPassword(password)
    hashedPasswordConfirm := hashPassword(passwordConfirm)


    if hashedPassword != hashedPasswordConfirm {
        return false
    }

    var lastInsertId int
    query := fmt.Sprintf("INSERT INTO users(username,password) VALUES('%s',$trick$%s$trick$) returning uid;", username, hashedPassword)
    err = db.QueryRow(query).Scan(&lastInsertId)

    fmt.Println(lastInsertId)

    if err != nil {
        return false;
    }
    return true;
}

Here is the table declaration

CREATE TABLE users
(
    uid SERIAL,
    username text NOT NULL UNIQUE,
    password text NOT NULL,
    weekly_goals bytea,
    CONSTRAINT users_pkey PRIMARY KEY (uid)
) WITH (OIDS=FALSE);
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!