0

I would like to convert integer ids in my database into something that is easier to remember.
Mapping function Int -> String should always return a unique 6 letter string for values in range, say, 1-10 000 000, with the same output for the same input. Example:

select old_id, gen_new_id(old_id)
from unnest(array[1, 2, 800, 289543, 1]) as foo(old_id)

could return

1, powaaa
2, iropwe
800, mnvfeq
289543, opasqa
1, powaaa

I could generate random string, check for uniqueness across the table and store as column in table, but it seems like someone before had to come up with better algorithmic approach.

3
  • loop doing mod 26 / div 26 generating 1 letter each loop. Commented Mar 1, 2022 at 14:38
  • Express the number in base 26 (a-z) or base 52 (a-zA-Z) instead of base 10. Adjust the range of numbers to ensure there are always six digits in the number. Commented Mar 1, 2022 at 14:48
  • Is 'mnvfeq' really easier to remember than '800'? Commented Mar 1, 2022 at 17:45

1 Answer 1

1

The technique I was looking for is named "ID obfuscating". There are various ways to accomplish the task in PostgresQL and here is a good article about it: https://czep.net/21/obfuscate.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.