• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

PostgreSQL Command: ALTER SEQUENCE

January 2, 2024

PostgreSQL Command: ALTER SEQUENCE

PostgreSQL is a powerful and feature-rich open-source relational database management system. It offers a wide range of commands and functions to manage and manipulate data effectively. One such command is ALTER SEQUENCE, which allows you to modify the attributes of a sequence in PostgreSQL.

What is a Sequence in PostgreSQL?

In PostgreSQL, a sequence is a database object that generates a sequence of unique numbers. It is often used to generate primary key values for tables automatically. Sequences are created using the CREATE SEQUENCE command and can be customized with various attributes.

Modifying a Sequence with ALTER SEQUENCE

The ALTER SEQUENCE command in PostgreSQL allows you to modify the attributes of an existing sequence. Here’s the basic syntax:

ALTER SEQUENCE sequence_name [ RESTART [ WITH ] restart_value ] [ INCREMENT [ BY ] increment ] [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ] [ START [ WITH ] start ] [ CACHE cache ] [ CYCLE | NO CYCLE ];

Let’s explore each attribute that can be modified using ALTER SEQUENCE:

RESTART [ WITH ] restart_value

This attribute allows you to restart the sequence with a new value. The restart_value specifies the new starting value for the sequence. For example, if you want to restart the sequence with a value of 100, you can use the following command:

ALTER SEQUENCE my_sequence RESTART WITH 100;

INCREMENT [ BY ] increment

This attribute allows you to change the increment value of the sequence. The increment specifies the amount by which the sequence value should be increased or decreased. For example, if you want to change the increment value to 2, you can use the following command:

ALTER SEQUENCE my_sequence INCREMENT BY 2;

MINVALUE minvalue | NO MINVALUE

This attribute allows you to set a minimum value for the sequence. The minvalue specifies the minimum value that the sequence can generate. If you want to remove the minimum value restriction, you can use NO MINVALUE. For example, to set the minimum value to 10, you can use the following command:

ALTER SEQUENCE my_sequence MINVALUE 10;

MAXVALUE maxvalue | NO MAXVALUE

This attribute allows you to set a maximum value for the sequence. The maxvalue specifies the maximum value that the sequence can generate. If you want to remove the maximum value restriction, you can use NO MAXVALUE. For example, to set the maximum value to 1000, you can use the following command:

ALTER SEQUENCE my_sequence MAXVALUE 1000;

START [ WITH ] start

This attribute allows you to change the current value of the sequence. The start specifies the new current value for the sequence. For example, if you want to set the current value to 50, you can use the following command:

ALTER SEQUENCE my_sequence START WITH 50;

CACHE cache

This attribute allows you to change the number of sequence values that are preallocated and stored in memory for faster access. The cache specifies the number of values to cache. For example, if you want to change the cache size to 100, you can use the following command:

ALTER SEQUENCE my_sequence CACHE 100;

CYCLE | NO CYCLE

This attribute allows you to specify whether the sequence should wrap around and start again from the beginning after reaching the maximum value (CYCLE) or stop generating values (NO CYCLE). For example, to enable cycling, you can use the following command:

ALTER SEQUENCE my_sequence CYCLE;

Conclusion

The ALTER SEQUENCE command in PostgreSQL provides a flexible way to modify the attributes of a sequence. Whether you need to change the starting value, increment, minimum or maximum value, current value, cache size, or cycling behavior, ALTER SEQUENCE has got you covered. By leveraging the power of ALTER SEQUENCE, you can easily customize and manage sequences in your PostgreSQL database.

Summary

In this article, we explored the PostgreSQL command ALTER SEQUENCE, which allows you to modify the attributes of a sequence. We discussed various attributes that can be modified, such as restarting the sequence, changing the increment, setting minimum and maximum values, modifying the current value, adjusting the cache size, and enabling cycling. To learn more about VPS hosting solutions, visit Server.HK.

Recent Posts

  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting
  • CentOS Stream Explained: Key Differences from CentOS Linux
  • How to Configure FirewallD in CentOS Stream: From Essential to Production-Grade
  • Installing Docker on CentOS: A Practical Setup Guide (CentOS Stream 9/10 – 2026)
  • How to Secure a CentOS Server: 15 Essential Hardening Techniques (CentOS Stream 9/10 – 2026)

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot