How to Fix Sequence “START Value can’t be Greater Than MAXVALUE” Error in PostgreSQL

Sequences are a widely used concept in PostgreSQL that allows us to create a unique series of integers. For this purpose, the CREATE SEQUENCE command is used in Postgres. The stated command supports various options, such as START, INCREMENT, MAXVALUE, etc. to specify the starting value, increment value, maximum value, etc. However, these options must be used carefully, otherwise, users may face undesired consequences.

This post will explain what are the possible reasons and suitable solutions for the “START Value can’t be Greater Than MAXVALUE” Error in PostgreSQL.

How to Fix Sequence “START Value can’t be Greater Than MAXVALUE” Error in Postgres?

While working with Postgres sequences, users may encounter the “START Value can’t be Greater Than MAXVALUE” error. It may be because of various reasons.

The possible reasons and respective solutions are described below:

Reason 1: Start Value is Greater Than the Maximum Value

At the time of sequence creation, the user specified a start value greater than the maximum value:

CREATE SEQUENCE example_1
INCREMENT 2
START 50
MAXVALUE 40;

In the above code snippet, the MAXVALUE is less than the start value, as a result, we will encounter an error stating that the start value can’t be greater than the maximum value:

img

Solution 1: Modify the Value of START and MAXVALUE Options

Make sure that the value of the “START” option is less than the “MAXVALUE” option. For example, to fix the stated error, we specify the value of the START option as less than the value of the MAXVALUE option:

CREATE SEQUENCE example_1
INCREMENT 2
START 40
MAXVALUE 60;
img

The error-free output indicates that the sequence has been successfully created.

Reason 2: Out of the Range Value

Specifying a value out of the range of the specified data type may also cause the stated problem. Consider the following snippet for a better understanding of this concept:

CREATE SEQUENCE example_1 AS SMALLINT
START 72000;

In the above code snippet, we created a sequence with the SMALLINT data type and set its initial value as “72000”, which is out of the range of the specified data type. Consequently, we will get the following result:

img

Solution 2: Use the Appropriate Data Type

Use an alternate data type that has a greater range or specify a value within the range of the specified data type. An example of fixing the stated error is provided in the following snippet:

CREATE SEQUENCE example_1 AS INTEGER
START 72000;

The value “72000” lies within the range of the “INT” data type. So, executing the above query will produce the following result:

img

The output shows that the sequence has been successfully created without causing any errors.

Conclusion

In Postgres, the “START Value can’t be Greater Than MAXVALUE” error occurs because of two reasons: if the start value is greater than the maximum value or if the user specified an out-of-the-range value. To fix the stated error, make sure that the value of the “START” option is less than the value of the “MAXVALUE” option. This post has explained a couple of fixes to rectify the “START Value can’t be Greater Than MAXVALUE” error in PostgreSQL.