I'm learning how to encrypt columns in postgresql and this is my code but i am getting an error when i try to insert values into the table, also i would like to make the function encrypt the data after insert. i dont know why is the reason that this error is coming up. if you guys could help me, i will really appreciate it. its for a school project that im putting together
-- dummy table
CREATE TABLE public.users
(
id_num smallint NOT NULL,
username Varchar (50) NOT NULL,
password Varchar (50) ,
test1 Varchar (50) ,
test2 Varchar (50) ,
test3 Varchar (50) ,
CONSTRAINT users_pkey PRIMARY KEY (id_num)
);
---encryption function
--postgresql trigger function
CREATE FUNCTION encrypt_solution_testing_function2()
RETURNS TRIGGER AS
$func$
DECLARE
BEGIN
new.username := PGP_SYM_ENCRYPT(new.username, 'sha1') ;
new.password := PGP_SYM_ENCRYPT(new.password , 'sha1');
new.test1 := PGP_SYM_ENCRYPT(new.test1 , 'sha1') ;
new.test2:= PGP_SYM_ENCRYPT(new.test2 , 'sha1') ;
new.test3 := PGP_SYM_ENCRYPT(new.test3 , 'sha1') ;
RETURN NEW;
END
$func$ LANGUAGE plpgsql;
--before insert trigger but would like to make it, after insert
CREATE TRIGGER encrypt_audit_log_testing2
BEFORE INSERT ON public.users
FOR EACH ROW EXECUTE PROCEDURE encrypt_solution_testing_function2();
error message:
ERROR: value too long for type character varying(50)
CONTEXT: PL/pgSQL function encrypt_solution_testing_function2() line 5 at assignment
SQL state: 22001