{"id":43588,"library":"pg-tables-to-zod","title":"pg-tables-to-zod","description":"A CLI tool and Node module (v1.0.24) that introspects PostgreSQL database schemas and generates Zod v4 type schemas for each table and view. It connects directly to a Postgres instance using pg-structure, outputs TypeScript files, and supports customizable configuration via JSON files. Unlike manual schema writing or ORM-based generators, it provides a focused, zero-runtime-dependency pipeline for keeping Zod definitions in sync with database schemas. Release cadence is irregular; last update was in 2025.","status":"active","version":"1.0.24","language":"javascript","source_language":"en","source_url":"https://github.com/SpringTree/pg-tables-to-zod","tags":["javascript","typescript"],"install":[{"cmd":"npm install pg-tables-to-zod","lang":"bash","label":"npm"},{"cmd":"yarn add pg-tables-to-zod","lang":"bash","label":"yarn"},{"cmd":"pnpm add pg-tables-to-zod","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Compile-time peer dependency for type generation","package":"typescript","optional":true}],"imports":[{"note":"Package is ESM-only. CommonJS require will fail unless using Node's .mjs extension or dynamic import.","wrong":"const convert = require('pg-tables-to-zod').convert","symbol":"convert","correct":"import { convert } from 'pg-tables-to-zod'"},{"note":"TConfiguration is a type, should be imported with 'type' for runtime efficiency. However, both work in TypeScript; the wrong pattern is only a linting concern, not a runtime error.","wrong":"import { TConfiguration } from 'pg-tables-to-zod'","symbol":"TConfiguration","correct":"import type { TConfiguration } from 'pg-tables-to-zod'"},{"note":"Same as TConfiguration – OutputSchema is a type. Use type import to avoid bundling issues.","wrong":"import { OutputSchema } from 'pg-tables-to-zod'","symbol":"OutputSchema","correct":"import type { OutputSchema } from 'pg-tables-to-zod'"}],"quickstart":{"code":"import { convert } from 'pg-tables-to-zod';\n\nconst configuration = {\n  connection: {\n    host: process.env.PG_HOST ?? 'localhost',\n    port: parseInt(process.env.PG_PORT ?? '5432'),\n    user: process.env.PG_USER ?? 'postgres',\n    password: process.env.PG_PASSWORD ?? '',\n    database: process.env.PG_DATABASE ?? 'mydb',\n  },\n  schema: ['public'],\n  outputPath: './schemas',\n  generateIndex: true,\n};\n\ntry {\n  const outputSchemas = await convert(configuration);\n  console.log(`Generated ${outputSchemas.length} schema files.`);\n} catch (error) {\n  console.error('Conversion failed:', error);\n}","lang":"typescript","description":"Connects to a Postgres database and generates Zod schemas for all tables in the public schema, writing them to ./schemas."},"warnings":[{"fix":"Ensure your project uses zod@^4.0.0. If using zod v3, the generated schemas will cause runtime errors.","message":"Zod v4 is required; the generated schemas use Zod v4 syntax (e.g., z.string().minLength(1)) which is incompatible with Zod v3.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Upgrade to zod v4 or use a different tool (e.g., schemats) for Zod v3 compatibility.","message":"The generated schemas are for Zod v4, which changed keyword casing (e.g., .minLength() instead of .min()). If you are on Zod v3, you must either upgrade or manually rewrite the output.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use await convert(configuration) or .then().","message":"The convert() function is async and returns an array of OutputSchema objects. Calling it without await will return a Promise, not the results.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'2025':85 'base':57 'cadenc':78 'cli':7 'configur':46 'connect':30 'customiz':45 'databas':16,75 'definit':71 'depend':66 'direct':31 'file':42,49 'focus':62 'generat':19,58 'instanc':35 'introspect':14 'irregular':80 'javascript':86 'json':48 'keep':69 'last':81 'manual':51 'modul':11 'node':10 'orm':56 'orm-bas':55 'output':40 'pg':2,38 'pg-structur':37 'pg-tables-to-zod':1 'pipelin':67 'postgr':34 'postgresql':15 'provid':60 'releas':77 'runtim':65 'schema':17,23,52,76 'structur':39 'support':44 'sync':73 'tabl':3,26 'tool':8 'type':22 'typescript':41,87 'unlik':50 'updat':82 'use':36 'v1.0.24':12 'v4':21 'via':47 'view':28 'write':53 'zero':64 'zero-runtime-depend':63 'zod':5,20,70","created_at":"2026-06-05T17:00:31.529966+00:00","updated_at":"2026-06-05T17:00:31.529966+00:00","problems":[{"fix":"Install the package with npm install pg-tables-to-zod (types are bundled). Ensure tsconfig.json has `moduleResolution: 'node'` (or 'node16' for ESM).","cause":"TypeScript cannot locate the module’s type definitions (missing ‘types’ field in package.json or not installed).","error":"Could not find a declaration file for module 'pg-tables-to-zod'."},{"fix":"Run npm install pg-tables-to-zod. Verify the package is in node_modules.","cause":"The package is not installed or the import path is incorrect.","error":"Cannot find module 'pg-tables-to-zod' or its corresponding type declarations."},{"fix":"Upgrade to zod v4: npm install zod@latest","cause":"The generated schemas import zod as default while your project has zod v3 which exports differently.","error":"TypeError: zod.z is not a function","affected_versions":">=1.0.0"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/SpringTree/pg-tables-to-zod#readme","github":"https://github.com/SpringTree/pg-tables-to-zod","docs":null,"changelog":null,"pypi":null,"npm":"pg-tables-to-zod","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database","devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}