How to create auto incremented alphanumeric id in postgres

Reading Time: < 1 minute

A few days ago, i got a condition where i need to create an alphanumeric auto incremented id in Postgres. I have created many auto incremented numeric ids before, but never got a use case like this. This was new for me.  Therefore, here i am going to share the solution for the same which can be useful for you as well.

First , we will create a sequence :

Then, if you want to start your id from a particular number, then do as follows :

Here, id will start from 101. If you will not use above line then it will start from 1.

Now define the column which you want to be alphanumeric auto incremented :

Here, id is the column name, while “RK” is the string which will be prefixed to auto incremented number.

Full code :

Now, run below command and see what happens :

A row will be inserted into employee table containing 2 as a id.

If you dont want to insert ids which does not have “RK” as prefix, then use the below line :

Full code for id condition check :

That’s all. Hope, it will help you guyz.

Cheers !!!



Written by 

Rishi is a Lead Consultant, with experience of more than 7 years. Rishi is product focused developer who loves developing both front-end user interfaces and scalable back-end infrastructure. He is a good team player, quick learner and a humble person. He has good time management skills, aimed to give best results and fully dedicated towards his work & responsibilities. He is able to work as individual and as well as in team. He loves to share his knowledge, therefore he often writes technical blogs.

1 thought on “How to create auto incremented alphanumeric id in postgres2 min read

Comments are closed.

%d bloggers like this: