Search code examples
sqlpostgresqlauto-increment

PostgreSQL equivalent of SQL Server's IDENTITY(1, 2)


Having this sample table:

create table testingCase (
id integer not null GENERATED ALWAYS AS IDENTITY,
constraint pk_testingCase primary key (id),
description varchar(60)
);

I want the id to be AUTO INCREMENTED by 2 (for example), in SQL Server that'd be IDENTITY (1, 2).

How can this be achieved utilizing PostgreSQL?


Solution

  • Use sequence options like in CREATE SEQUENCE.

    create table testing_case (
        id integer not null generated always as identity (increment by 2),
        constraint pk_testing_case primary key (id),
        description varchar(60)
    );
    
    insert into testing_case (description) 
    values ('a'), ('b'), ('c')
    returning *
    
     id | description 
    ----+-------------
      1 | a
      3 | b
      5 | c
    (3 rows)