Registry / database / pg-ts-gen

pg-ts-gen

JSON →
library4.2.0jsnpmunverified

Generate TypeScript interfaces and constants from a Postgres database schema by querying pg_catalog. Version 4.2.0 requires TypeScript >=4.1. A fork of schemats that drops MySQL support in favor of deeper Postgres integration, supports table/column comments as JSDoc, custom JSON types via `--jsonTypesFile`, date-as-string mode, and schema name prefixing. Ships with a CLI tool and outputs both select and input interfaces plus runtime table constants.

npm install pg-ts-gen
INSTALL
IMPORT
SIG · PG-TS-GEN
P
pg-ts-gen
databasejavascriptv4.2.0
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.

TableTypes
import { TableTypes } from './dbschema'
import { TableTypes } from 'pg-to-ts'
The generated file is local, not a package export.
tables
import { tables } from './dbschema'
const { tables } = require('./dbschema')
Generated file uses ESM; CJS require breaks if module is compiled to CJS, use ESM imports.
Product
import { Product } from './dbschema'
import { Product } from 'pg-to-ts'
Types are generated into a local file, not from the package.

Connects to Postgres and generates TypeScript interfaces and constants into dbschema.ts.

npx pg-to-ts generate -c postgresql://user:pass@localhost:5432/mydb -o dbschema.ts
Debug
Known issues
breakingNode.js >=8.15.1 required; older versions will fail with syntax errors or missing features.
fix
Upgrade Node.js to >=8.15.1 or use nvm to switch.
affects: <8.15.1
breakingPeer dependency TypeScript >=4.1 required; generation may fail silently or produce invalid code with older TypeScript.
fix
Install or upgrade TypeScript to >=4.1.
affects: <4.1
gotchaDropped MySQL support; only Postgres is supported. Running against MySQL will produce incorrect output or errors.
fix
Use a different tool for MySQL schema generation.
affects: all
gotchaTimestamp columns are typed as Date by default, but JSON serialization converts them to strings, causing type mismatch at runtime.
fix
Use --datesAsStrings flag if you prefer string types, and ensure pg types return strings.
affects: all
deprecatedThe --jsonTypesFile flag expects a relative path without file extension. Using an extension may cause module resolution failure.
fix
Omit file extension in --jsonTypesFile value, e.g., --jsonTypesFile './db-types'
affects: >=4.0
Errors
Common errors & fixes
Unknown option: --jsonTypesFile
Typo in option name (e.g., --jsonTypeFile missing 's').
fix
Use --jsonTypesFile (with 's').
Cannot find module './dbschema'
Generated file not compiled or path incorrect.
fix
Ensure dbschema.ts is included in your tsconfig.json files or use correct relative path.
pg-to-ts: command not found
Package not installed globally or locally.
fix
Run via npx: npx pg-to-ts generate ...
Upgrade
Version history
4.2.0latest on npm
Audit
Dependencies
typescriptrequiredPeer dependency for type generation
Agent activity
4 hits · last 30 days
node
4
Resources
pg-ts-gen — npm install pg-ts-gen · libregistry