pgjq

Use jq in Postgres

Overview

PackageVersionCategoryLicenseLanguage
pgjq0.1.0UTILMITC
IDExtensionBinLibLoadCreateTrustRelocSchema
4150pgjqNoYesNoYesYesYes-
Relatedpgjwt pg_protobuf jsquery sparql gzip bzip zstd http

build with jq-devel

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY0.1.01817161514pgjq-
RPMPIGSTY0.1.01817161514pgjq_$v-
DEBPIGSTY0.1.01817161514postgresql-$v-pgjq-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
u24.x86_64
u24.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0

Build

You can build the RPM / DEB packages for pgjq using pig build:

pig build pkg pgjq         # build RPM / DEB packages

Install

You can install pgjq directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pgjq;          # Install for current active PG version
pig ext install -y pgjq -v 18  # PG 18
pig ext install -y pgjq -v 17  # PG 17
pig ext install -y pgjq -v 16  # PG 16
pig ext install -y pgjq -v 15  # PG 15
pig ext install -y pgjq -v 14  # PG 14
dnf install -y pgjq_18       # PG 18
dnf install -y pgjq_17       # PG 17
dnf install -y pgjq_16       # PG 16
dnf install -y pgjq_15       # PG 15
dnf install -y pgjq_14       # PG 14
apt install -y postgresql-18-pgjq   # PG 18
apt install -y postgresql-17-pgjq   # PG 17
apt install -y postgresql-16-pgjq   # PG 16
apt install -y postgresql-15-pgjq   # PG 15
apt install -y postgresql-14-pgjq   # PG 14

Create Extension:

CREATE EXTENSION pgjq;

Usage

pgjq: Use jq JSON processing language in PostgreSQL

Provides a jqprog data type for jq programs and a jq() function to execute them on jsonb objects.

Basic Filtering

SELECT jq('[{"bar": "baz", "balance": 7.77}]'::jsonb, '.[0].bar');
-- "baz"

Using the @@ Operator

SELECT '[{"bar": "baz"}]' @@ '.[0].bar'::jqprog;
-- "baz"

Complex Programs

SELECT jq('[true,false,[5,true,[true,[false]],false]]',
          '(..|select(type=="boolean")) |= if . then 1 else 0 end');
-- [1, 0, [5, 1, [1, [0]], 0]]

SELECT jq('[1,5,3,0,7]', '(.[] | select(. >= 2)) |= empty');
-- [1, 0]

Passing Arguments

Pass dynamic arguments as a jsonb object, referenced as $var:

SELECT jq(
    '{"jobs": [{"id": 9, "ok": true}, {"id": 100, "ok": false}]}'::jsonb,
    '.jobs[] | select(.ok == $ok and .id == 100) | .',
    '{"ok": false}'
);

Chaining with jsonpath

SELECT jq('[{"cust":"baz","active":true,"trans":{"balance":100}}]',
          '(.[] | select(.active == true))') - '{trans}' @> '{"cust": "baz"}';
-- t

Working with Files

SELECT jq(pg_read_file('/path/to/data.json'), '.[]');

Last Modified 2026-03-12: add pg extension catalog (95749bf)