pg_jsonschema
pg_jsonschema
pg_jsonschema : PostgreSQL extension providing JSON Schema validation
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 2760 | pg_jsonschema | pg_jsonschema | 0.3.4 | FEAT | Apache-2.0 | Rust |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
----d-- | No | No | No | Yes | no | no |
| Relationships | |
|---|---|
| See Also | pg_graphql jsquery plv8 jsonb_plperl http pg_net pg_summarize pg_tiktoken |
manual update from 0.16.0 by Vonng
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 0.3.4 | 18 17 16 15 14 | pg_jsonschema | - |
| RPM | PIGSTY | 0.3.4 | 18 17 16 15 14 | pg_jsonschema_$v | - |
| DEB | PIGSTY | 0.3.4 | 18 17 16 15 14 | postgresql-$v-pg-jsonschema | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
el8.aarch64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
el9.x86_64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
el9.aarch64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
el10.x86_64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
el10.aarch64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
d12.x86_64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
d12.aarch64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
d13.x86_64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
d13.aarch64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
u22.x86_64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
u22.aarch64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
u24.x86_64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
u24.aarch64 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 | PIGSTY 0.3.4 |
Source
pig build pkg pg_jsonschema; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pg_jsonschema; # install via package name, for the active PG version
pig install pg_jsonschema -v 18; # install for PG 18
pig install pg_jsonschema -v 17; # install for PG 17
pig install pg_jsonschema -v 16; # install for PG 16
pig install pg_jsonschema -v 15; # install for PG 15
pig install pg_jsonschema -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION pg_jsonschema;Usage
pg_jsonschema: PostgreSQL extension providing JSON Schema validation
pg_jsonschema adds JSON Schema validation functions to PostgreSQL, enabling schema enforcement on JSON/JSONB columns via check constraints.
Functions
| Function | Description |
|---|---|
json_matches_schema(schema json, instance json) | Validate a JSON instance against a schema, returns boolean |
jsonb_matches_schema(schema json, instance jsonb) | Validate a JSONB instance against a schema, returns boolean |
jsonschema_is_valid(schema json) | Check whether a JSON schema itself is valid |
jsonschema_validation_errors(schema json, instance json) | Return an array of validation error messages |
Table Constraints
Use check constraints to enforce document structure:
CREATE TABLE customer (
id serial PRIMARY KEY,
metadata json,
CHECK (
json_matches_schema(
'{
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"type": "string",
"maxLength": 16
}
}
}
}',
metadata
)
)
);
-- Valid insert (passes check constraint)
INSERT INTO customer(metadata) VALUES ('{"tags": ["vip", "darkmode-ui"]}');
-- Invalid insert (rejected by check constraint)
INSERT INTO customer(metadata) VALUES ('{"tags": [1, 3]}');
-- ERROR: new row violates check constraintError Inspection
Retrieve detailed validation errors:
SELECT jsonschema_validation_errors('{"maxLength": 4}', '"123456789"');
-- Returns: {"\"123456789\" is longer than 4 characters"}Schema Validation
Verify that a schema is well-formed before using it:
SELECT jsonschema_is_valid('{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0}
},
"required": ["name"]
}');
-- Returns: trueLast updated on