ALTER SEQUENCE

An SQL command to modify the definition of a sequence

ALTER SEQUENCE is a DDL command to modify the definition of a sequence.

ALTER SEQUENCE was added in PostgreSQL 7.4.

Change history

Examples

Basic usage example for ALTER SEQUENCE:

postgres=# ALTER SEQUENCE foo_id_seq RENAME TO bar_id_seq;
ALTER SEQUENCE

Changing the sequence data type:

postgres=# ALTER SEQUENCE bar_id_seq AS integer;
ALTER SEQUENCE

The data type cannot be changed if the sequence's current value exceeds the maximum value of the new data type:

postgres=# CREATE SEQUENCE type_test_seq AS bigint;
CREATE SEQUENCE

postgres=# ALTER SEQUENCE type_test_seq RESTART 32768;
ALTER SEQUENCE

postgres=# ALTER SEQUENCE type_test_seq AS smallint;
ERROR:  RESTART value (32768) cannot be greater than MAXVALUE (32767)

Categories

DDL, Sequences, SQL command

See also

CREATE SEQUENCE, DROP SEQUENCE