Open In App

How to Add an Identity to an Existing Column in PostgreSQL?

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

PostgreSQL, a robust open-source relational database management system, offers a variety of tools for managing and organizing data. One such feature is the ability to add an identity to an existing column, which is particularly useful in situations when each row requires a unique identifier. In this article, we’ll look at how to add identity to an existing column in PostgreSQL, including a step-by-step instruction and practical examples.

PostgreSQL excels in adding identity to existing columns, providing unique identifiers for efficient data management. Using the ALTER TABLE statement, users can customize identity columns, elevating data organization and uniqueness in this powerful relational database management system.

Adding Identity to an Existing Column in PostgreSQL

To add an identity to an existing column, we can use the ALTER TABLE statement with the ADD GENERATED clause. This procedure allows you to define a column as an identity column, and PostgreSQL will produce unique values for that column depending on the conditions you specify.

Syntax:

To add an identity to an existing column, use the code below:

ALTER TABLE table_name
ALTER COLUMN column_name
ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( identity_options ) ]


Let’s understand the main elements of the syntax:

  • table_name: The table containing the column.
  • column_name: The column to which the identity will be added.
  • GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY: It specifies the generation method for the identity column.
  • identity_options: Additional options such as START WITH, INCREMENT BY, and MINVALUE/MAXVALUE.

Now, let’s take a look at some examples to do the task.

Examples

Example 1: Let’s create a table named employee and check the default value for the columns.

-- create
CREATE TABLE EMPLOYEE (
emp_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL
);

\d EMPLOYEE;


After describing the table columns, there is no default identity set for the columns.

 Table "public.employee"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
emp_id | integer | | not null |
name | text | | not null |
dept | text | | not null |
Indexes:
"employee_pkey" PRIMARY KEY, btree (emp_id)


In the initial setup, a table named EMPLOYEE was created with columns emp_id (integer), name (text), and dept (text). No default identity was set for the columns.

Now, let’s use the above syntax to set the IDENTITY Column for the employee table.

-- create table employee
CREATE TABLE EMPLOYEE (
emp_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL
);

ALTER TABLE EMPLOYEE
ALTER COLUMN emp_id
ADD GENERATED BY DEFAULT AS IDENTITY;

\d EMPLOYEE;


Now let’s describe the above table:

 Table "public.employee"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+----------------------------------
emp_id | integer | | not null | generated by default as identity
name | text | | not null |
dept | text | | not null |
Indexes:
"employee_pkey" PRIMARY KEY, btree (emp_id)


Hence, emp_id is set as the IDENTITY column for the table.

Example 2: Suppose we want an identity column named emp_id in the EMPLOYEE table, starting from 1000 and incrementing by 5.

-- create table employee
CREATE TABLE EMPLOYEE (
emp_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL
);

ALTER TABLE EMPLOYEE
ALTER COLUMN emp_id
ADD GENERATED BY DEFAULT AS IDENTITY
(START WITH 1000 INCREMENT BY 5);

INSERT into EMPLOYEE (name, dept) values ('Ben', 'HR');

SELECT * from EMPLOYEE;


Now, let’s check the creation of auto generated value:

 emp_id | name | dept 
--------+------+------
1000 | Ben | HR


The output displays the “EMPLOYEE” table with an identity column “emp_id” starting at 1000 and incrementing by 5. One record is inserted, showing emp_id 1000, name ‘Ben,’ and department ‘HR.’

Conclusion

In conclusion, PostgreSQL’s capability to add an identity to an existing column provides valuable functionality for managing unique identifiers. Utilizing the ALTER TABLE statement with the ADD GENERATED clause allows for the efficient customization of identity columns to meet specific requirements. This feature enhances data organization and uniqueness, contributing to PostgreSQL’s effectiveness as a robust relational database management system.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads