pgtyped
PgTyped is a TypeScript code generator that automatically creates type-safe SQL query interfaces by introspecting a live PostgreSQL database. It lets developers write raw SQL while maintaining full type safety for parameters and results, eliminating manual schema-to-TypeScript mapping.
Key facts
Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.
| Field | Value |
|---|---|
| Repository | adelsz/pgtyped |
| Owner | adelsz |
| Primary language | TypeScript |
| License | MIT — OSI-approved |
| Stars | 3.3k |
| Forks | 113 |
| Open issues | 96 |
| Latest release | v2.4.3 (2025-03-15) |
| Last updated | 2026-07-08 |
| Source | https://github.com/adelsz/pgtyped |
What pgtyped is
PgTyped parses SQL and TypeScript files to extract query definitions, connects to a running PostgreSQL instance to infer types from the schema, and generates PreparedQuery wrappers with strict TypeScript interfaces. It supports watch mode, array/object parameter helpers, and prevents SQL injection by delegating parameter substitution to the PostgreSQL server.
Get the pgtyped source
Clone the repository and explore it locally.
git clone https://github.com/adelsz/pgtyped.gitcd pgtyped# follow the project's README for install & configurationNeed it deployed, integrated, or customized instead? DEV.co ships production installs.
Best use cases
Implementation considerations
- Requires TypeScript as a peer dependency; not usable in pure JavaScript projects.
- Setup demands a running, accessible PostgreSQL instance during code generation; configure connection details in config.json early.
- Watch mode during development is convenient but requires the database to remain available; plan for CI/CD to have consistent DB access.
- Generated files are source-controlled artifacts; integrate code generation into build/pre-commit workflows to keep them synchronized.
- SQL query annotations (@name directives) are mandatory; establish naming and file organization conventions for the team.
When to avoid it — and what to weigh
- Multi-database requirement — PgTyped is PostgreSQL-only. Projects needing MySQL, SQL Server, or other databases require alternative tooling.
- Database-first development without live schema access — Type generation requires a running PostgreSQL instance with live schema. CI/CD pipelines or offline development without stable DB access will face friction.
- Teams preferring schema-as-code patterns — If your team uses migrations or schema definition tools (Prisma, Drizzle, SQLAlchemy) as the source of truth rather than the live database, PgTyped's runtime introspection may conflict with your workflow.
- Unstable or frequently changing query patterns — Projects with highly dynamic or user-generated SQL will struggle; PgTyped is designed for static, pre-defined queries in files.
License & commercial use
MIT License. Permissive OSI-approved license permitting commercial use, modification, and redistribution with attribution. See LICENSE file in repo for full text.
MIT is a permissive license allowing commercial use without restriction. No license review is required for closed-source or commercial projects. Attribution in documentation is customary but not legally required.
DEV.co evaluation signals
Editorial assessment — not user reviews. Directional, with an explicit confidence level.
| Signal | Assessment |
|---|---|
| Maintenance | Active |
| Documentation | Strong |
| License clarity | Clear |
| Deployment complexity | Low |
| DEV.co fit | Strong |
| Assessment confidence | High |
PgTyped mitigates SQL injection by never performing string interpolation; parameters are sent separately to PostgreSQL for safe server-side substitution. Database credentials must be managed securely in config.json; avoid committing connection strings. Generated code itself is not cryptographically validated; review generated files before committing if source queries are untrusted. Dependency supply-chain risk is moderate given active maintenance.
Alternatives to consider
Prisma
Schema-as-code, multi-database support, built-in migrations. Trade-off: heavier runtime, less raw SQL flexibility.
Drizzle ORM
Lightweight TypeScript-first SQL builder with type inference, ESM-native. Trade-off: DSL instead of raw SQL; smaller ecosystem.
sqlc (Go, or sqlc-js if available)
Similar concept: SQL files → typed code generation. Trade-off: primarily Go ecosystem; JavaScript support evolving.
Build on pgtyped with DEV.co software developers
Evaluate PgTyped for your PostgreSQL backend. We can help you architect the integration, set up code generation pipelines, and train your team on best practices for SQL-first type-safe development.
Talk to DEV.coRelated open-source tools
Surfaced by semantic similarity across the DEV.co open-source index.
Related on DEV.co
Explore the category and the services that help you build with it.
pgtyped FAQ
Does PgTyped work with migrations or schema version control?
Can I use PgTyped in a serverless environment?
What happens to generated code if I rename or modify a query?
Is PgTyped suitable for teams without deep SQL knowledge?
Custom software development services
DEV.co helps companies turn open-source tools like pgtyped into production software. Our software development services cover the full lifecycle — architecture, web development, integration, and maintenance — delivered by software developers and web developers who ship. Engage our software development agency to implement or customize it for your open-source databases stack.
Ready to add type-safe SQL to your TypeScript stack?
Evaluate PgTyped for your PostgreSQL backend. We can help you architect the integration, set up code generation pipelines, and train your team on best practices for SQL-first type-safe development.