Registry / database / postgres-schema-ts

postgres-schema-ts

JSON →
library0.5.0jsnpmunverified

Convert PostgreSQL schemas into TypeScript interfaces. v0.5.0 is the latest stable release. This package provides a CLI and programmatic API to generate TypeScript type definitions from Postgres database schemas. It focuses on simplicity with no configuration required, supporting both full schema inference and per-table generation. Unlike alternatives like schemats, it is Postgres-only, uses inline enums, and avoids namespaces. The generation respects PostgreSQL defaults such as nullable columns (producing union with null) and timestamp mapping to Date. No recent updates since 2022.

npm install postgres-schema-ts
INSTALL
IMPORT
SIG · POSTGRES-SCHEMA-TS
P
postgres-schema-ts
databasejavascriptv0.5.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.

inferSchema
import { inferSchema } from 'postgres-schema-ts'
import inferSchema from 'postgres-schema-ts'
Named export, not default export. ESM-only since package uses ES modules.
inferTable
import { inferTable } from 'postgres-schema-ts'
const { inferTable } = require('postgres-schema-ts')
CommonJS require works if using Node 12+ with ESM compatibility, but ESM import is recommended.
CLI command
npx postgres-schema-ts postgresql://user:pass@host:port/db?currentSchema=public
postgres-schema-ts postgres://user:pass@host:port/db
Connection string uses postgresql:// protocol, not postgres://. The currentSchema query parameter is required for schema selection.

Demonstrates both CLI usage and programmatic import to generate TypeScript interfaces from a Postgres schema connection string.

// Generate TypeScript types from a Postgres schema // Using CLI: // npx postgres-schema-ts postgresql://postgres:password@localhost:5433/mydb?currentSchema=public // Using programmatic API: import { inferSchema } from 'postgres-schema-ts'; const connectionString = 'postgresql://postgres:password@localhost:5433/mydb?currentSchema=public'; async function main() { try { const types = await inferSchema(connectionString); console.log(types); } catch (err) { console.error('Error generating types:', err); } } main(); // Output is a string with TypeScript interfaces. // Example output for a 'users' table: // export interface users { // id: number; // name: string; // email: string | null; // created_at: Date; // }
Debug
Known issues
gotchaConnection string must use 'postgresql://' protocol, not 'postgres://'.
fix
Use postgresql://user:pass@host/db instead of postgres://user:pass@host/db
affects: >=0.0.0
gotchaThe 'currentSchema' query parameter is required in connection string to specify schema.
fix
Add '?currentSchema=public' (or your schema) to the connection string
affects: >=0.0.0
gotchaCLI output goes to stdout; use shell redirection to save to file.
fix
npx postgres-schema-ts <args> > schema.ts
affects: >=0.0.0
breakingIn v0.5.0, the library switched to ES modules only; CommonJS require may fail in older Node versions.
fix
Use import syntax or set 'type': 'module' in package.json or use dynamic import.
affects: >=0.5.0
gotchaNullable columns are typed as 'Type | null' rather than 'Type | undefined'.
fix
If you prefer undefined, manually adjust generated types.
affects: >=0.0.0
deprecatedThe package has not been updated since 2022, and may not support modern PostgreSQL features.
fix
Consider alternatives like schemats or pg-to-ts if newer features are needed.
affects: <=0.5.0
Errors
Common errors & fixes
Error: connect ECONNREFUSED 127.0.0.1:5433
Connection string points to wrong port or database not running.
fix
Ensure PostgreSQL is running on the specified port (default 5432) and correct connection string.
Error: getaddrinfo ENOTFOUND localhost
Hostname 'localhost' not resolved; may be Docker or network issue.
fix
Use explicit IP 127.0.0.1 or host.docker.internal if in Docker.
SyntaxError: Cannot use import statement outside a module
Running TypeScript/ESM code without proper module configuration.
fix
Add 'type': 'module' to package.json, or use .mjs extension, or compile with TypeScript.
error: conversion between types is not supported
Unsupported PostgreSQL column type encountered.
fix
Manually adjust the generated interface or file an issue on GitHub.
Upgrade
Version history
0.5.0latest on npm
Audit
Dependencies
pgrequiredPostgreSQL client used to query schema information from the database
Agent activity
7 hits · last 30 days
node
6
Resources
postgres-schema-ts — npm install postgres-schema-ts · libregistry