DROP SEQUENCE

An SQL command for removing a sequence

DROP SEQUENCE is a DDL command for removing a sequence.

DROP SEQUENCE was added in PostgreSQL 6.4.

Change history

Examples

Basic usage example for DROP SEQUENCE:

postgres=# DROP SEQUENCE foo_id_seq;
DROP SEQUENCE

Dropping a sequence which may not exist:

postgres=# DROP SEQUENCE IF EXISTS foo_id_seq;
NOTICE:  sequence "foo_id_seq" does not exist, skipping
DROP SEQUENCE

Attempting to drop a sequence which is associated with a table column:

postgres=# CREATE TABLE bar (id SERIAL);
CREATE TABLE

postgres=# \d bar
                            Table "public.bar"
 Column |  Type   | Collation | Nullable |             Default             
--------+---------+-----------+----------+---------------------------------
 id     | integer |           | not null | nextval('bar_id_seq'::regclass)

postgres=# DROP SEQUENCE bar_id_seq;
ERROR:  cannot drop sequence bar_id_seq because other objects depend on it
DETAIL:  default value for column id of table bar depends on sequence bar_id_seq
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

Forcibly dropping a sequence and its dependencies:

postgres=# DROP SEQUENCE bar_id_seq CASCADE;
NOTICE:  drop cascades to default value for column id of table bar
DROP SEQUENCE

postgres=# \d bar
                Table "public.bar"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 id     | integer |           | not null | 

Categories

DDL, Sequences, SQL command

See also

CREATE SEQUENCE, ALTER SEQUENCE