Randomized Database Ids

2 min readMar 15, 2022


Get random integer ids instead of sequential ones.

Disclaimer: This was a proof of concept just for fun and entertainment. Could you not bring it into production? 😅

UUID is the perfect solution, but I was constrained to do it with the integer.

I wondered how we could make a kind ids obfuscator based on database functions.

I tried with Mysql, but not much luck here. Mysql is harsh 😅

However, with Postgresql, I got something working.

Create a function to generate a random integer to apply in between two records.

Create the function to get the new id value+ a random value

Add a trigger to call the function.

and call the trigger on the tables you want to randomize

Do not forget to remove the previous id sequence!

The complete working code for Postgresql is here.

Mysql attempts here.

Who knows someone can make use of that 😅