Registry / database / pg-tables-to-jsonschema

pg-tables-to-jsonschema

JSON →
library1.1.3jsnpmunverified

A command-line utility and Node.js module to convert PostgreSQL table definitions into JSON Schema (Draft-04/07 compatible). Version 1.1.3 is the latest stable release, with active but infrequent updates. Uses pg-structure internally for schema introspection. Supports multiple PostgreSQL schemas, table inclusion/exclusion, custom base URLs, and config files. Less feature-rich than alternatives like 'db-to-jsonschema' but focused on simplicity and direct PostgreSQL integration. Written in TypeScript, ships type definitions.

npm install pg-tables-to-jsonschema
INSTALL
IMPORT
SIG · PG-TABLES-TO-JSONS
P
pg-tables-to-jsonschema
databasejavascriptv1.1.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

converter
import { converter } from 'pg-tables-to-jsonschema'
const converter = require('pg-tables-to-jsonschema')
Package exports a named export 'converter', not a default export. CommonJS require returns an object with the export as a property, but the named import is preferred.
default
import pgTablesToJsonschema from 'pg-tables-to-jsonschema'
Using default import will give you the `converter` function; it's sugar over the named export. Both are valid in TypeScript and ESM.
TableToJsonschemaOptions
import { TableToJsonschemaOptions } from 'pg-tables-to-jsonschema'
TypeScript users should import this type for configuration objects. Not available in older CJS-only setups.

Demonstrates how to import the converter function, connect to a PostgreSQL database, and convert tables to JSON Schema with custom options.

import { converter } from 'pg-tables-to-jsonschema' const schemas = await converter({ pg: { host: process.env.PG_HOST ?? 'localhost', port: parseInt(process.env.PG_PORT ?? '5432'), user: process.env.PG_USER ?? 'admin', password: process.env.PG_PASSWORD ?? 'secret', database: process.env.PG_DATABASE ?? 'my_db' }, input: { schemas: ['public'], exclude: [], include: [] }, output: { additionalProperties: false, baseUrl: 'http://api.localhost.com/schema/', indentSpaces: 2, outDir: './schemas', unwrap: false } }) console.log(JSON.stringify(schemas, null, 2))
Debug
Known issues
breakingVersion 1.0.0 introduced breaking changes: switched from 'pg' to 'pg-structure', configuration object format changed. Old v0.x configs will not work.
fix
Update configuration to match the new format with 'pg', 'input', 'output' keys. See README or migration guide.
affects: <1.0.0
deprecatedThe CLI option '--pg-host' and others are superseded by '--config' file support. Using individual options without a config file may be deprecated in future releases.
fix
Use a configuration file (JSON) with the '--config' option for more reliable behavior.
affects: >=1.1.0
gotchaThe package uses pg-structure which may have different default naming or column type mappings. Some PostgreSQL types like arrays, enum, or custom domains may produce incomplete schemas or throw errors.
fix
Check pg-structure documentation or your table definitions. Consider custom transformations if needed.
affects: >=1.0.0
Errors
Common errors & fixes
Error: password authentication failed for user "myuser"
Incorrect PostgreSQL credentials or missing pg_hba.conf configuration.
fix
Verify your --pg-user and --pg-password options, or use a .pgpass file. Ensure pg_hba.conf allows password authentication.
TypeError: converter is not a function
Using CommonJS require incorrectly. The export is named 'converter', not default.
fix
Use `const { converter } = require('pg-tables-to-jsonschema')` instead of `const converter = require('pg-tables-to-jsonschema')`.
Could not find any tables in schema 'public'
Specified schema does not exist or user lacks permissions. PostgreSQL schema names are case-sensitive unless quoted.
fix
Check schema name spelling and quotes. Ensure the database user has USAGE and SELECT privileges on the schema and tables.
Upgrade
Version history
1.1.3latest on npm
Audit
Dependencies
pg-structurerequiredCore dependency for connecting to PostgreSQL and introspecting table structure.
commanderrequiredUsed for CLI argument parsing.
Agent activity
2 hits · last 30 days
node
2
Resources
pg-tables-to-jsonschema — npm install pg-tables-to-jsonschema · libregistry