PostgreSQL – Identity Column
Last Updated :
14 Jul, 2024
In PostgreSQL, the GENERATED AS IDENTITY constraint is used to automatically generate unique values for a column. It simplifies the management of auto-incrementing columns, similar to the SERIAL column but adhering to SQL standards.
Let us get a better understanding of the GENERATED AS IDENTITY constraint in PostgreSQL from this article.
Syntax
column_name type GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY[ ( sequence_option ) ]Parameters:
Let’s analyze the above syntax.
column_name: The name of the column to be defined as an identity column.type: Specifies the data type of the column, which can be SMALLINT, INT, BIGINT, or another compatible data type.GENERATED ALWAYS: Instructs PostgreSQL to always generate a value for the identity column. Any attempt to insert or update a value into this column will result in an error unless using OVERRIDING SYSTEM VALUE.GENERATED BY DEFAULT: Specifies that PostgreSQL should generate a value for the identity column, but allows inserting or updating specific values into the column. If a value is provided, it will be used instead of the system-generated value.
PostgreSQL Identity Column Examples
Now let’s look into some examples of Identity Column in PostgreSQL to better understand the concept.
Example 1: Using GENERATED ALWAYS
Create a table named ‘color' with ‘color_id' as an identity column and insert a row:
PostgreSQL
CREATE TABLE color (
color_id INT GENERATED ALWAYS AS IDENTITY,
color_name VARCHAR NOT NULL
);
INSERT INTO color (color_name)
VALUES
('Red');
SELECT * FROM color;
Because ‘color_id’ column has the GENERATED AS IDENTITY constraint, PostgreSQL generates a value for it as shown in the query below.
Output:

Insert a new row by providing values for both ‘color_id’ and ‘color_name’ columns:
INSERT INTO color (color_id, color_name)
VALUES (2, 'Green');
PostgreSQL issued the following error:
[Err] ERROR: cannot insert into column "color_id"
DETAIL: Column "color_id" is an identity column defined as GENERATED ALWAYS.
HINT: Use OVERRIDING SYSTEM VALUE to override.

To fix the error, in this case, you can use the OVERRIDING SYSTEM VALUE clause as follows:
INSERT INTO color (color_id, color_name)
OVERRIDING SYSTEM VALUE
VALUES (2, 'Green');
Now if we use the below statement to verify the entry:
SELECT * FROM color;
Output:

Example 2: Using GENERATED BY DEFAULT
In this example, we will use the GENERATED BY DEFAULT AS IDENTITY to create the same table we created above. To do so, drop the color table as below:
PostgreSQL
DROP TABLE color;
CREATE TABLE color (
color_id INT GENERATED BY DEFAULT AS IDENTITY,
color_name VARCHAR NOT NULL
);
INSERT INTO color (color_name)
VALUES
('White');
INSERT INTO color (color_id, color_name)
VALUES
(2, 'Yellow');
SELECT * FROM color;
Output:

Here unlike the previous example that uses the GENERATED ALWAYS AS IDENTITY constraint, this statement also works.
Important Points About PostgreSQL GENERATED AS IDENTITY constraint
- PostgreSQL’s
GENERATED AS IDENTITY is compliant with the SQL standard, providing a consistent and portable way to create auto-incrementing columns. - GENERATED ALWAYS ensures that PostgreSQL always generates a unique value for the column.
- GENERATED BY DEFAULT allows you to insert specific values if needed. If no value is provided, PostgreSQL will generate one.
- Identity columns are optimized for performance, particularly in high-concurrency environments.