📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PostgreSQL Essentials Array Operations

Array Operations

5 min read
Array columns store multiple values of the same type. Use @> to check containment and unnest() to expand into rows. Add a GIN index for fast array containment queries. Great for storing tags and labels.

Arrays in PostgreSQL

CREATE TABLE tags (
  id   SERIAL PRIMARY KEY,
  post TEXT,
  tags TEXT[]
);

INSERT INTO tags (post, tags)
VALUES ('Hello', ARRAY['php','tutorial']);

SELECT * FROM tags WHERE 'php' = ANY(tags);
SELECT array_length(tags, 1) FROM tags;
SELECT unnest(tags) FROM tags; -- expand to rows