Sobes.tech
Junior — Middle

What types of fields in PostgreSQL automatically receive an index when creating a database?

sobes.tech AI

Answer from AI

In PostgreSQL, indexes are automatically created for columns with unique constraints and primary keys. That is, when you create a column with a PRIMARY KEY or UNIQUE constraint, an index is automatically created for that column to ensure quick lookups and enforce uniqueness.

Example:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,  -- index created automatically for id
    email VARCHAR(255) UNIQUE  -- index created for email
);

Regular columns without such constraints are not automatically indexed.