info > CREATE_SEQUENCE

📝 NAME

CREATE_SEQUENCE - define a new sequence generator

🚀 Quick Reference

Use Case Command Description
Create ascending sequence CREATE SEQUENCE serial START 101; 📈 Creates a new ascending sequence starting at 101
Get next value SELECT nextval('serial'); ⏩ Advances the sequence and returns the new value
Set current value SELECT setval('serial', 42); 🎯 Sets the sequence's current value
Create descending sequence CREATE SEQUENCE serial2 INCREMENT -1; 📉 Creates a new descending sequence
Use in INSERT INSERT INTO t VALUES (nextval('serial'), 'data'); ➕ Uses sequence as default value in a row

📖 SYNOPSIS

CREATE [ TEMPORARY | TEMP ] SEQUENCE [ IF NOT EXISTS ] name
    [ AS data_type ]
    [ INCREMENT [ BY ] increment ]
    [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
    [ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]
    [ OWNED BY { table_name.column_name | NONE } ]

📄 DESCRIPTION

CREATE SEQUENCE creates a new sequence number generator. 🏗️ This involves creating and initializing a new special single-row table with the name name. The generator will be owned by the user issuing the command.

If a schema name is given then the sequence is created in the specified schema. Otherwise it is created in the current schema. Temporary sequences exist in a special schema, so a schema name cannot be given when creating a temporary sequence. The sequence name must be distinct from the name of any other sequence, table, index, view, or foreign table in the same schema.

After a sequence is created, you use the functions nextval, currval, and setval to operate on the sequence. 🔧 These functions are documented in Section 9.17.

Although you cannot update a sequence directly, you can use a query like:

SELECT * FROM name;

to examine the parameters and current state of a sequence. In particular, the last_value field of the sequence shows the last value allocated by any session. ⚠️ (Of course, this value might be obsolete by the time it's printed, if other sessions are actively doing nextval calls.)

⚙️ PARAMETERS

📝 NOTES

💡 EXAMPLES

Create an ascending sequence called serial, starting at 101:

CREATE SEQUENCE serial START 101;

Select the next number from this sequence:

SELECT nextval('serial');
 nextval
--------
    101

Select the next number from this sequence:

SELECT nextval('serial');
 nextval
--------
    102

Use this sequence in an INSERT command:

INSERT INTO distributors VALUES (nextval('serial'), 'nothing');

Update the sequence value after a COPY FROM:

BEGIN;
COPY distributors FROM 'input_file';
SELECT setval('serial', max(id)) FROM distributors;
END;

🤝 COMPATIBILITY

CREATE SEQUENCE conforms to the SQL standard, with the following exceptions:

🔗 SEE ALSO

ALTER SEQUENCE, DROP SEQUENCE

CREATE_SEQUENCE
📝 NAME 🚀 Quick Reference 📖 SYNOPSIS 📄 DESCRIPTION ⚙️ PARAMETERS 📝 NOTES 💡 EXAMPLES 🤝 COMPATIBILITY 🔗 SEE ALSO

Generated by phpman v4.9.26-5-g7740029 Author: Che Dong Under GNU General Public License
2026-08-14 20:16 @2600:1f28:365:80b0:4d23:66fa:c2bb:7bae
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!