How to Create PostgreSQL Leading Zero Sequence (zerofill)

Vasilen Donchev picture Vasilen Donchev · Jul 28, 2011 · Viewed 15.7k times · Source

How can I create a leading zero sequence in PostgreSQL?

For MySQL I know it is BIGINT(10) UNSIGNED ZEROFILL AUTO_INCREMENT but in PostgreSQL I can't find an equivalent (only bigserial).

Moreover how can I limit the number of zeroes as BIGINT(10) means 10 symbols, does the type bigserial have such a limit?

Answer

m1tk4 picture m1tk4 · Jul 28, 2011

Create a regular sequence, then use to_char() to pad it with leading zeroes. Your data type will be char(), though, Postgres does not support zerofill for integral types.