How to short UUIDs with Postgres?



  • I see that many web services (Stripe comes to mind) use a special encoding for their UUIDs. Instead of the usual encoding a44521d0-0fb8-4ade-8002-3385545c3318 they are going to be encoded using a special alphabet (usually lowercase, uppercase letters and numbers), which results in a UUID like mhvXdrZT4jP5T8vBxuvm75. Both represent the same UUID but with a different encoding.

    I'm wondering how these services handle these IDs? Is it possible with Postgres in particular to directly use this short ID encoding in the database? (in other words, any select or insert would use the short ID)

    Or would it make sense to save these short IDs directly as text in the database?

    I can't find much info about this so I'm not sure what's the best practice is here.



  • UUIDs are in fact 16 byte long binary strings and you normally want to store and manipulate them as such. Only when passing UUIDs via a protocol that is not capable of dealing with binary strings (e.g. HTTP queries) you need to serialize them as character strings. The two common methods you are referring to in your question are hexadecimal representation (a44521d0-0fb8-4ade-8002-3385545c3318) and base-64 encoding (mhvXdrZT4jP5T8vBxuvm75). Pick the one that you like and that is compatible with upstream and downstream applications.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2