How to use pgcrypto `crypt` function with Go's (*sql.Tx).Exec function?

135 Views Asked by At

I would like to be able to use the encryption provided by the PostgreSQL extension pgcrypto from within my go program. Consider this code snippet:

_, err = tx.Exec("INSERT INTO users (username, email, password) VALUES (?, ?, crypt(?, gen_salt('bf', 8)))", u.Username, u.Email, u.Password)
    if err != nil {
        fmt.Println("Could not create user in database:", err.Error())
        return err
    }

I receive the following log message: Could not create user in database: pq: syntax error at or near ",".

When I manually execute this statement

INSERT INTO users(username, email, password) VALUES ('test', '[email protected]', crypt('12345', gen_salt('bf', 8)));

it successfully creates a row in the database. How can I make the crypt function work from within Go?

0

There are 0 best solutions below