Store and query JSON in PostgreSQL

8 steps · about 2 h 20 min · Intermediate

  1. Choose between json and jsonb (article, PostgreSQL docs, 20 min)

    Explains storage differences and containment.

  2. Insert JSON documents (article, PostgreSQL docs, 10 min)

    Shows how to load rows with JSON values.

  3. Use JSON operators, functions and paths (article, PostgreSQL docs, 35 min)

    Reference for ->, ->>, @> and SQL/JSON path queries.

  4. Expand arrays into rows (article, PostgreSQL docs, 15 min)

    Uses generate_series-style functions to unnest data.

  5. Index JSON with GIN (article, PostgreSQL docs, 20 min)

    Makes containment queries fast.

  6. Index one JSON key with an expression index (article, PostgreSQL docs, 15 min)

    A cheaper index when you query a single key.

  7. Use array functions beside JSON arrays (article, PostgreSQL docs, 10 min)

    Gives the array tools to compare with jsonb array handling.

  8. Update rows holding JSON (article, PostgreSQL docs, 15 min)

    Pairs UPDATE with jsonb_set for field changes.