Here's a sane function that picks from allowed characters:
CREATE OR REPLACE FUNCTION random_string(int) RETURNS TEXT as $$
select
string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as random_word
from (values('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')) as symbols(characters)
join generate_series(1, $1) on 1 = 1
$$ language sql;
Then use it as:
UPDATE mytable SET col1 = random_string(10), col2 = random_string(20);
Minimal test:
CREATE OR REPLACE FUNCTION random_string(int) RETURNS TEXT as $$
select
string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as random_word
from (values('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 --')) as symbols(characters)
join generate_series(1, $1) on 1 = 1
$$ language sql;
DROP TABLE IF EXISTS tmp;
CREATE TABLE "tmp" ("i" INTEGER, "j" INTEGER, "s" TEXT, "t" TEXT);
INSERT INTO "tmp" (i, j, s, t) SELECT i, i*2, 'a', 'b' FROM generate_series(1, 10) as s(i);
SELECT * FROM "tmp";
UPDATE "tmp" SET s = random_string(10), t = random_string(20);
SELECT * FROM "tmp";
which outputs:
CREATE FUNCTION
DROP TABLE
CREATE TABLE
INSERT 0 10
i | j | s | t
----+----+---+---
1 | 2 | a | b
2 | 4 | a | b
3 | 6 | a | b
4 | 8 | a | b
5 | 10 | a | b
6 | 12 | a | b
7 | 14 | a | b
8 | 16 | a | b
9 | 18 | a | b
10 | 20 | a | b
(10 rows)
UPDATE 10
i | j | s | t
----+----+------------+----------------------
1 | 2 | pqb0jVp i | PImey082XovRskbK5mxY
2 | 4 | DqOtVlf5r4 | 13MPe1WAiTi4Pr pEGHK
3 | 6 | AITONX Xzg | VTU4gKsN4fuoRR8dVb7o
4 | 8 | PcmsD5t1g- | JV4ohJ DtKGKwc kRGJ
5 | 10 | oJ-RtapI-q | G XBIP2UqGpxOSroY3s7
6 | 12 | ScecWoJ6jy | JDWdjTFBm0rseuVwqdJa
7 | 14 | 3bigPU7GHG | 1u VEgNIhXYf ZZa7z2W
8 | 16 | 4vLHduh- | Zk20QXq1t Jb2fevaQ
9 | 18 | sW t7Jzr3v | Cvr3aD wd H8jdgHvSq
10 | 20 | F3ylfYcqe4 | 0ccHaM9XW-Qzg2tV-gI0
(10 rows)
so we see that both columns were updated with different values each time.
As per my quick benchmark at: SQL Populate table with random data it is about 10x slower than md5(random()::text.