PostgreSQL: Create an auto incrementing Primary Key using a Custom Sequence

In this post, I am creating an auto increment PRIMARY KEY using a custom sequence of the PostgreSQL.

PostgreSQL already provides two data types for auto increment id, serial (4 bytes) & bigserial (8 bytes).
When we apply serial or bigserial to the particular column, internally it creates a default sequence object for that column and automatically it generates auto increment value for that column.

We can also create custom sequence object in the PostgreSQL and it also requires in some situation where we do not want to use default sequence value.

First, Create a sequence object:Starting with 100 and increment by 10

Create sample table:

Use this sequence object and Insert sample data:

Anvesh Patel

Leave a Reply

1 Comment on "PostgreSQL: Create an auto incrementing Primary Key using a Custom Sequence"

Notify of
avatar
Sort by:   newest | oldest | most voted
Bruno Felipe
Guest

Nice post, thanks for sharing it

wpDiscuz