PostgreSQL – Get current value and next value of a Sequence

Postgres provides currval and nextval functions for sequence manipulation.

currval function returns the current value of the sequence without advancing the sequence.

nextval function advances the sequence and returns the next value.

Syntax:

SELECT currval('sequencename');
SELECT nextval('sequencename');

Below is the syntax to use sequence in the insert statements where sequencename is the name of the sequence.

INSERT INTO tablename(col1,col2,col3) values (nextval('sequencename'),val2, val3);
See also  PostgreSQL - DISTINCT ON clause