PostgreSQL – How to Move Sequence to a Different Schema
To move a sequence to a different schema in PostgreSQL, you can use the ALTER SEQUENCE
command. The basic syntax is as follows:
ALTER SEQUENCE current_schema.sequence_name SET SCHEMA new_schema;
where current_schema
is the name of the schema where the sequence currently resides, sequence_name
is the name of the sequence, and new_schema
is the name of the schema where you want to move the sequence.For example, if you want to move a sequence named “my_sequence” from the “public” schema to a schema called “new_schema,” you would use the following SQL command:
ALTER SEQUENCE public.my_sequence SET SCHEMA new_schema;
Verify the Move:
After executing the command, you can verify that the sequence has been moved to the new schema by querying the information_schema.sequences
view or by using the \d
command in the PostgreSQL command line interface.
For example, you can use the following SQL query to check the schema of the sequence:
SELECT sequence_schema FROM information_schema.sequences WHERE sequence_name = 'my_sequence';
This query should return the name of the new schema where the sequence now resides.