{
    "mode": "man",
    "parameter": "CREATE_SEQUENCE",
    "section": "7",
    "url": "https://www.chedong.com/phpMan.php/man/CREATE_SEQUENCE/7/json",
    "generated": "2026-07-05T13:27:11Z",
    "synopsis": "CREATE [ TEMPORARY | TEMP ] SEQUENCE [ IF NOT EXISTS ] name\n[ AS datatype ]\n[ INCREMENT [ BY ] increment ]\n[ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]\n[ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]\n[ OWNED BY { tablename.columnname | NONE } ]",
    "sections": {
        "NAME": {
            "content": "CREATESEQUENCE - define a new sequence generator\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "CREATE [ TEMPORARY | TEMP ] SEQUENCE [ IF NOT EXISTS ] name\n[ AS datatype ]\n[ INCREMENT [ BY ] increment ]\n[ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]\n[ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]\n[ OWNED BY { tablename.columnname | NONE } ]\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "CREATE SEQUENCE creates a new sequence number generator. This involves creating and\ninitializing a new special single-row table with the name name. The generator will be owned\nby the user issuing the command.\n\nIf a schema name is given then the sequence is created in the specified schema. Otherwise it\nis created in the current schema. Temporary sequences exist in a special schema, so a schema\nname cannot be given when creating a temporary sequence. The sequence name must be distinct\nfrom the name of any other sequence, table, index, view, or foreign table in the same schema.\n\nAfter a sequence is created, you use the functions nextval, currval, and setval to operate on\nthe sequence. These functions are documented in Section 9.17.\n\nAlthough you cannot update a sequence directly, you can use a query like:\n\nSELECT * FROM name;\n\nto examine the parameters and current state of a sequence. In particular, the lastvalue\nfield of the sequence shows the last value allocated by any session. (Of course, this value\nmight be obsolete by the time it's printed, if other sessions are actively doing nextval\ncalls.)\n",
            "subsections": []
        },
        "PARAMETERS": {
            "content": "TEMPORARY or TEMP\nIf specified, the sequence object is created only for this session, and is automatically\ndropped on session exit. Existing permanent sequences with the same name are not visible\n(in this session) while the temporary sequence exists, unless they are referenced with\nschema-qualified names.\n\nIF NOT EXISTS\nDo not throw an error if a relation with the same name already exists. A notice is issued\nin this case. Note that there is no guarantee that the existing relation is anything like\nthe sequence that would have been created — it might not even be a sequence.\n\nname\nThe name (optionally schema-qualified) of the sequence to be created.\n\ndatatype\nThe optional clause AS datatype specifies the data type of the sequence. Valid types are\nsmallint, integer, and bigint.  bigint is the default. The data type determines the\ndefault minimum and maximum values of the sequence.\n\nincrement\nThe optional clause INCREMENT BY increment specifies which value is added to the current\nsequence value to create a new value. A positive value will make an ascending sequence, a\nnegative one a descending sequence. The default value is 1.\n\nminvalue\nNO MINVALUE\nThe optional clause MINVALUE minvalue determines the minimum value a sequence can\ngenerate. If this clause is not supplied or NO MINVALUE is specified, then defaults will\nbe used. The default for an ascending sequence is 1. The default for a descending\nsequence is the minimum value of the data type.\n\nmaxvalue\nNO MAXVALUE\nThe optional clause MAXVALUE maxvalue determines the maximum value for the sequence. If\nthis clause is not supplied or NO MAXVALUE is specified, then default values will be\nused. The default for an ascending sequence is the maximum value of the data type. The\ndefault for a descending sequence is -1.\n\nstart\nThe optional clause START WITH start allows the sequence to begin anywhere. The default\nstarting value is minvalue for ascending sequences and maxvalue for descending ones.\n\ncache\nThe optional clause CACHE cache specifies how many sequence numbers are to be\npreallocated and stored in memory for faster access. The minimum value is 1 (only one\nvalue can be generated at a time, i.e., no cache), and this is also the default.\n\nCYCLE\nNO CYCLE\nThe CYCLE option allows the sequence to wrap around when the maxvalue or minvalue has\nbeen reached by an ascending or descending sequence respectively. If the limit is\nreached, the next number generated will be the minvalue or maxvalue, respectively.\n\nIf NO CYCLE is specified, any calls to nextval after the sequence has reached its maximum\nvalue will return an error. If neither CYCLE or NO CYCLE are specified, NO CYCLE is the\ndefault.\n\nOWNED BY tablename.columnname\nOWNED BY NONE\nThe OWNED BY option causes the sequence to be associated with a specific table column,\nsuch that if that column (or its whole table) is dropped, the sequence will be\nautomatically dropped as well. The specified table must have the same owner and be in the\nsame schema as the sequence.  OWNED BY NONE, the default, specifies that there is no such\nassociation.\n",
            "subsections": []
        },
        "NOTES": {
            "content": "Use DROP SEQUENCE to remove a sequence.\n\nSequences are based on bigint arithmetic, so the range cannot exceed the range of an\neight-byte integer (-9223372036854775808 to 9223372036854775807).\n\nBecause nextval and setval calls are never rolled back, sequence objects cannot be used if\n“gapless” assignment of sequence numbers is needed. It is possible to build gapless\nassignment by using exclusive locking of a table containing a counter; but this solution is\nmuch more expensive than sequence objects, especially if many transactions need sequence\nnumbers concurrently.\n\nUnexpected results might be obtained if a cache setting greater than one is used for a\nsequence object that will be used concurrently by multiple sessions. Each session will\nallocate and cache successive sequence values during one access to the sequence object and\nincrease the sequence object's lastvalue accordingly. Then, the next cache-1 uses of nextval\nwithin that session simply return the preallocated values without touching the sequence\nobject. So, any numbers allocated but not used within a session will be lost when that\nsession ends, resulting in “holes” in the sequence.\n\nFurthermore, although multiple sessions are guaranteed to allocate distinct sequence values,\nthe values might be generated out of sequence when all the sessions are considered. For\nexample, with a cache setting of 10, session A might reserve values 1..10 and return\nnextval=1, then session B might reserve values 11..20 and return nextval=11 before session A\nhas generated nextval=2. Thus, with a cache setting of one it is safe to assume that nextval\nvalues are generated sequentially; with a cache setting greater than one you should only\nassume that the nextval values are all distinct, not that they are generated purely\nsequentially. Also, lastvalue will reflect the latest value reserved by any session, whether\nor not it has yet been returned by nextval.\n\nAnother consideration is that a setval executed on such a sequence will not be noticed by\nother sessions until they have used up any preallocated values they have cached.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "Create an ascending sequence called serial, starting at 101:\n\nCREATE SEQUENCE serial START 101;\n\nSelect the next number from this sequence:\n\nSELECT nextval('serial');\n\nnextval\n---------\n101\n\nSelect the next number from this sequence:\n\nSELECT nextval('serial');\n\nnextval\n---------\n102\n\nUse this sequence in an INSERT command:\n\nINSERT INTO distributors VALUES (nextval('serial'), 'nothing');\n\nUpdate the sequence value after a COPY FROM:\n\nBEGIN;\nCOPY distributors FROM 'inputfile';\nSELECT setval('serial', max(id)) FROM distributors;\nEND;\n",
            "subsections": []
        },
        "COMPATIBILITY": {
            "content": "CREATE SEQUENCE conforms to the SQL standard, with the following exceptions:\n\n•   Obtaining the next value is done using the nextval() function instead of the standard's\nNEXT VALUE FOR expression.\n\n•   The OWNED BY clause is a PostgreSQL extension.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "ALTER SEQUENCE (ALTERSEQUENCE(7)), DROP SEQUENCE (DROPSEQUENCE(7))\n\n\n\nPostgreSQL 14.23                                2026                              CREATE SEQUENCE(7)",
            "subsections": []
        }
    },
    "summary": "CREATESEQUENCE - define a new sequence generator",
    "flags": [],
    "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 'inputfile';",
        "SELECT setval('serial', max(id)) FROM distributors;",
        "END;"
    ],
    "see_also": [
        {
            "name": "ALTERSEQUENCE",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/ALTERSEQUENCE/7/json"
        },
        {
            "name": "DROPSEQUENCE",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/DROPSEQUENCE/7/json"
        },
        {
            "name": "SEQUENCE",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/SEQUENCE/7/json"
        }
    ]
}