While working with the Postgres sequences, users often encounter a “MINVALUE must be less than MAXVALUE” error. This error occurs at the time of sequence creation. A common issue that causes the stated error is setting inappropriate values for the “MINVALUE” and “MAXVALUE” options while creating a sequence.
This article will present an easy fix to rectify the “MINVALUE must be less than MAXVALUE” error in Postgres.
How to Resolve “MINVALUE must be less than MAXVALUE” Error in Postgres?
As the name itself depicts, the stated error occurs in Postgres if a user specifies the value of the “MINVALUE” option is more than or equal to the “MAXVALUE” option. The below snippet will help you understand how this error occurs in Postgres:
CREATE SEQUENCE example_sequence MINVALUE 100 MAXVALUE 100;
In the above snippet, we encounter an error because the value of MINVALUE and MAXVALUE were equal. Similarly, if the “MAXVALUE” is less than the “MINVALUE”, then we will face the same error as follows:
In the case of descending sequence, we can also experience the same error if we didn't specify the MAXVALUE explicitly:
CREATE SEQUENCE example_sequence INCREMENT -3 MINVALUE 10;
This error can be resolved by increasing the value of the “MAXVLAUE” option and decreasing the value of the “MINVALUE” option or both at the same time:
CREATE SEQUENCE example_sequence MINVALUE 100 MAXVALUE 200;
The above output proves that increasing the value of the “MAXVALUE” option resolves the stated error. Similarly, in the case of descending sequence, specifying the MAXVALUE option will resolve this error:
CREATE SEQUENCE example_sequence_1 INCREMENT -3 MINVALUE 10 MAXVALUE 100;
The stated error has been rectified successfully.
Conclusion
In PostgreSQL, the “MINVALUE must be less than MAXVALUE” error can be resolved by increasing the value of the “MAXVLAUE” option and decreasing the value of the “MINVALUE” option or both at the same time. In the case of descending sequence, we can also experience the same error if we didn't specify the MAXVALUE explicitly. However, it can be fixed by explicitly specifying the MAXVALUE. This post explained how to fix the “MINVALUE must be less than MAXVALUE” error in Postgres.