Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random String in PostgreSQL

I'm using this SQL query to generate random value in PostgreSQL

chr(ascii('B') + (random() * 25)::integer)

How I can generate 15 characters random String using the same query?

like image 200
Peter Penzov Avatar asked Apr 10 '16 17:04

Peter Penzov


People also ask

What is MD5 in PostgreSQL?

The PostgreSQL MD5() function is used to evaluate the MD5 hash of a string and subsequently return the result. The result is generally in hexadecimal form. Syntax: MD5(string) Let's analyze the above syntax: The string argument is the string of which the MD5 hash is calculated.


1 Answers

Another solution that's pretty easy to read (perf should be reasonable, but no benchmarks were performed):

select substr(md5(random()::text), 0, 25);

Could be uppercased if you prefer:

select upper(substr(md5(random()::text), 0, 25));
like image 129
fncomp Avatar answered Sep 22 '22 23:09

fncomp