# pg_jsonschema — Postgres extension

> Check json and jsonb values against a JSON Schema in a CHECK constraint.

Source: https://scribase.com/extensions/pg_jsonschema

Category: APIs and HTTP. Version: 0.3.4. Optional, enabled per project.

## About

pg_jsonschema adds json_matches_schema and jsonb_matches_schema. Use them in a CHECK constraint to keep a flexible jsonb column from drifting: inserts that do not match the schema fail with a constraint violation (SQLSTATE 23514).

## Enable it

```sql
create extension if not exists pg_jsonschema with schema extensions;
```

## Example

```sql
alter table products add constraint attributes_shape check (
  jsonb_matches_schema(
    '{"type": "object",
      "properties": {"color": {"type": "string"}, "size": {"type": "integer"}},
      "required": ["color"]}',
    attributes
  )
);
```

Upstream: https://github.com/supabase/pg_jsonschema