PostgreSQL - UNIQUE Index

Last Updated : 1 Aug, 2026

UNIQUE index in PostgreSQL ensures that all values in the indexed column (or combination of columns) are unique.

  • Prevent duplicate values in a column.
  • Enforce data integrity and Improve query performance.
  • Create unique indexes on single or multiple columns.

Syntax

CREATE UNIQUE INDEX index_name
ON table_name (column_name);

Where:

  • index_name: The name of the unique index.
  • table_name: The table on which the index is created.
  • column_name: The column or columns whose values must remain unique.

Working

Consider the following Employees Table for the examples below:

Screenshot-2026-07-31-101232
Employees Table

Example 1: Create a UNIQUE Index on a Single Column

The following statement creates a unique index on the Email column.

Query:

CREATE UNIQUE INDEX idx_email
ON Employees (Email);

Output:

Screenshot-2026-07-31-101340

The statement creates a unique index on the Email column, preventing duplicate email addresses.

Example 2: Create a UNIQUE Index on Multiple Columns

The following statement creates a unique index on the Department and Email columns.

Query:

CREATE UNIQUE INDEX idx_department_email
ON Employees (Department, Email);

Output:

Screenshot-2026-07-31-101340

Example 3: Insert a Duplicate Value

The following statement attempts to insert a duplicate email address.

Query:

INSERT INTO Employees
VALUES
(105, 'Olivia Johnson', 'HR', 'john@example.com');

Output:

Screenshot-2026-07-31-101428
Comment

Explore