PostgreSQL – Create Auto-increment Column using SERIAL
Last Updated :
22 Aug, 2024
In PostgreSQL, sequences play a vital role when it comes to generating unique identifiers, especially for primary key columns. A sequence is a special database object that generates a sequence of integers, which can be used as unique IDs across rows in a table. In this article, we’ll look into how sequences work, the usage of the SERIAL pseudo-type, and explore examples to better understand how PostgreSQL handles sequences.
What is a Sequence in PostgreSQL?
A sequence is a database object designed to generate a series of unique integers. These are particularly useful for automatically populating a primary key column. When defining a primary key column, PostgreSQL offers a shortcut through the SERIAL pseudo-type, which simplifies sequence creation and management.
What is the SERIAL Pseudo-Type?
The SERIAL pseudo-type is a convenient way to create auto-incrementing primary keys. PostgreSQL offers three variants of the SERIAL type:
- SMALLSERIAL: Uses 2 bytes and stores values from 1 to 32,767.
- SERIAL: Uses 4 bytes and stores values from 1 to 2,147,483,647.
- BIGSERIAL: Uses 8 bytes and stores values from 1 to 9,223,372,036,854,775,807.
When using the SERIAL type, PostgreSQL automatically handles sequence creation, setting the next integer as the default value for the column.
Syntax:
CREATE TABLE table_name(
id SERIAL
);
In the above syntax by setting the SERIAL pseudo-type to the id column, PostgreSQL performs the following:
- First, create a sequence object and set the next value generated by the sequence as the default value for the column.
- Second, add a NOT NULL constraint to the id column because a sequence always generates an integer, which is a non-null value.
- Third, assign the owner of the sequence to the id column; as a result, the sequence object is deleted when the id column or table is dropped.
The above syntax is equivalent to the below statement:
CREATE SEQUENCE table_name_id_seq;
CREATE TABLE table_name (
id integer NOT NULL DEFAULT nextval('table_name_id_seq')
);
ALTER SEQUENCE table_name_id_seq
OWNED BY table_name.id; PostgreSQL provides three serial pseudo-types SMALLSERIAL, SERIAL, and BIGSERIAL with the following characteristics:
| Name | Storage Size | Range |
|---|
| SMALLSERIA | 2 bytes | 1 to 32, 767 |
| SERIAL | 4 bytes | 1 to 2, 147, 483, 647 |
| BIGSERIAL | 8 bytes | 1 to 9, 223, 372, 036, 854, 775, 807 |
Now let’s look into an example for better understanding.
Create Auto-increment Column using SERIAL Examples
Let us look at some of the examples to better understand the concept in PostgreSQL.
Example 1: Using the SERIAL Type
The following statement creates the ‘animals' table with the ‘id' column as the SERIAL column:
PostgreSQL
CREATE TABLE animals(
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL
);
INSERT INTO animals(name)
VALUES('Dog');
INSERT INTO animals(name)
VALUES('Cat');
SELECT * FROM animals;
Output:

Example 2: Returning the Generated ID
The following statement inserts a new row into the ‘animals' table and returns the value generated for the id column:
INSERT INTO animals(name)
VALUES('Tiger')
RETURNING id; Output:

Best Practices When Using SERIAL in PostgreSQL
- While sequences are unique, they should not be used as meaningful data. Gaps and resets can occur.
- If your system requires unique IDs across multiple databases or instances, using UUIDs may be a better option than relying on SERIAL.
- For extremely large datasets, the choice between SERIAL and BIGSERIAL can impact performance. Always benchmark according to your workload.