Registry / database / pg-typegen

pg-typegen

JSON →
library3.9.1jsnpmunverified

Generate TypeScript type definitions from a Postgres database schema. v3.9.1 — actively maintained, weekly releases. Connects directly to a Postgres instance via connection string and introspects tables, columns, enums, views, and materialized views. Supports configurable suffixes, custom schemas, insert types, comments, and multiple output styles (interfaces/types, semicolons, optionals). Key differentiator: maps Postgres types to native JavaScript/TypeScript types matching the pg driver output, with bigint, date, and enum handling. No ORM dependency.

npm install pg-typegen
INSTALL
IMPORT
SIG · PG-TYPEGEN
P
pg-typegen
databasejavascriptv3.9.1
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.

pg-typegen CLI
npx pg-typegen postgres://user:pass@localhost:5432/db
npx pg-typegen postgres://localhost:5432/db
Connection string must include credentials. Use npx or install globally.
Unknown import usage
This package is a CLI tool, not a library. No programmatic API is exported. Use npx or install globally and run from command line.
Generated types
import { UserEntity, UserState } from './entities'
import UserEntity from './entities'
Generated file will have named exports (interface and enum). Use named imports based on table/type names.

Generates TypeScript type definitions from a Postgres database schema, outputting to entities.ts.

// Generate types from Postgres database npx pg-typegen -o ./entities.ts postgres://username:password@localhost:5432/database
Debug
Known issues
gotchaConnection string must contain credentials; otherwise it may hang or fail. Use postgres://user:password@host:port/db format.
fix
Ensure connection string includes username and password, e.g., postgres://admin:secret@localhost:5432/mydb
affects: >=0.0.0
gotchapg-typegen does not handle bigint types by default; int8 columns map to string instead of number.
fix
Use --bigint flag to treat int8 as bigint (JavaScript BigInt) instead of string.
affects: <3.0.0
gotchaGenerated types for nullable columns include '| null' by default. Use --optionals flag to change to optional '?'.
fix
Add --optionals flag to get optional fields instead of nullable union types.
affects: >=0.0.0
deprecatedUse of interfaces vs types: default generates interfaces. Use --type flag to generate type aliases.
fix
Add --type flag if you prefer type aliases over interfaces.
affects: >=0.0.0
Errors
Common errors & fixes
Error: connect ECONNREFUSED ::1:5432
PostgreSQL server not running or connection string uses wrong host/port.
fix
Verify PostgreSQL is running and accessible. Check host, port, and firewall settings.
error: password authentication failed for user "..."
Invalid username or password in connection string.
fix
Update connection string with correct credentials. For local dev, consider using peer authentication.
Error: Cannot find module 'pg'
pg-typegen requires 'pg' as a peer dependency; likely missing.
fix
Run: npm install pg --save-dev
Upgrade
Version history
3.9.1latest on npm
Audit
Dependencies
commanderrequiredCLI argument parsing
pgrequiredPostgreSQL client for connection and schema introspection
Agent activity
67 hits · last 30 days
node
42
Bingbot
25
Resources
pg-typegen — npm install pg-typegen · libregistry