Skip to content

pgnats

NATS messaging integration with publish, subscribe, JetStream, key-value, and object store SQL APIs

pgnats · NATS messaging integration with publish, subscribe, JetStream, key-value, and object store SQL APIs

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100725pgnatspgnats2.1.0ETLMITRust

PostgreSQL · 14, 15, 16, 17, 18

Relationships
Requiresplpgsql

Usage

Sources:

pgnats 2.1.0 connects PostgreSQL to NATS publish/request messaging, JetStream, key-value and object storage. It supports PostgreSQL 15–18; PostgreSQL 14 needs an older release. Subscription and storage APIs depend on the enabled build features.

Enable Messaging

Subscription background workers require preload. Preserve other entries and restart PostgreSQL after changing the setting:

shared_preload_libraries = 'pgnats.so'

A superuser installs the extension and configures its single allowed foreign server:

CREATE EXTENSION pgnats;
CREATE SERVER nats_fdw_server
  FOREIGN DATA WRAPPER pgnats_fdw
  OPTIONS (host 'nats.example.net', port '4222', capacity '128');
SELECT nats_publish_jsonb('events.order.created', '{"id":42}'::jsonb);
SELECT nats_request_text('service.echo', 'ping', 1000);

The extension is not trusted or relocatable. Preload is specifically needed for subscription workers; simple messaging functions do not by themselves establish a running subscription service. Restrict NATS subjects and credentials with broker-side ACLs and configure TLS as appropriate.

Permissions and Callbacks

Version 2.1.0 requires a superuser for subscription management and FDW configuration. A subscription callback must accept exactly one bytea input, be owned by a superuser, and reside in a superuser-controlled schema. Its identity and ownership are rechecked before privileged worker execution. Review existing callbacks and subscriptions before upgrading.

Ordinary SQL data-access roles need function EXECUTE and USAGE on the configured foreign server. An administrator can grant the server permission to an existing role:

GRANT USAGE ON FOREIGN SERVER nats_fdw_server TO application_role;

Role changes and grants or revocations are checked for data operations. Access through an unconfigured localhost connection is restricted to superusers. Database privileges do not replace NATS authorization, and message payloads remain untrusted application input.

API and Resource Boundaries

The API includes text, bytea and JSONB publish/request functions, JetStream operations, key-value and object-store functions, plus subscribe/unsubscribe support when built. Match callback and data types to the installed signatures. Remote effects are not automatically undone by a PostgreSQL rollback; design idempotency and delivery handling at the application boundary.

The release fixes shared-memory queue bounds, removes eager allocation based on remote object metadata and clears cached object-store handles on connection reload. These fixes do not establish unlimited message sizes or a complete security guarantee.

Upgrade to 2.1.0

Install matching library and SQL files for the server, then restart PostgreSQL to replace preloaded workers and libraries. The 2.0.0-to-2.1.0 script preserves SQL signatures and objects:

ALTER EXTENSION pgnats UPDATE TO '2.1.0';

Keep the supplied build configuration for its dependency mitigation, and verify application-role grants, callback ownership and message delivery after upgrading. Older installations must have the complete upgrade chain available.

Table reader