DEV.co
Open-Source Databases · adelsz

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.

Source: GitHub — github.com/adelsz/pgtyped
3.3k
GitHub stars
113
Forks
TypeScript
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositoryadelsz/pgtyped
Owneradelsz
Primary languageTypeScript
LicenseMIT — OSI-approved
Stars3.3k
Forks113
Open issues96
Latest releasev2.4.3 (2025-03-15)
Last updated2026-07-08
Sourcehttps://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.

Quickstart

Get the pgtyped source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/adelsz/pgtyped.gitcd pgtyped# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

TypeScript applications requiring SQL type-safety

Applications where developers want to write idiomatic SQL without ORM overhead while maintaining full compile-time type checking for query parameters and result sets.

PostgreSQL-centric backends with complex queries

Services with non-trivial SQL (CTEs, window functions, complex joins) that benefit from live schema introspection rather than hand-maintained ORMs or type definitions.

Developer-focused teams migrating from untyped query builders

Teams using raw pg client or query builders who want to reduce runtime type errors and improve IDE autocomplete without rewriting queries.

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.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityLow
DEV.co fitStrong
Assessment confidenceHigh
Security considerations

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.

Software development agency

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.co

Related 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?
PgTyped introspects the live database schema at code generation time. It pairs well with migration tools (Flyway, Knex migrations) as long as migrations run before code generation. If schema definitions are version-controlled separately (e.g., Prisma schema), you manage synchronization manually.
Can I use PgTyped in a serverless environment?
Code generation (CLI) requires a live database connection and is typically run during build, not at runtime. Serverless functions themselves run generated query code fine via pg driver and connection pooling (e.g., PgBouncer). Generation in CI/CD is standard practice.
What happens to generated code if I rename or modify a query?
You must manually update or regenerate the .queries.ts files. Watch mode (-w flag) auto-regenerates on SQL/TS file changes, but you must manage the @name annotations and file organization. Generated imports elsewhere in your codebase may break; a refactoring tool or careful grep is recommended.
Is PgTyped suitable for teams without deep SQL knowledge?
PgTyped assumes developers can write PostgreSQL SQL. It reduces type boilerplate but does not hide SQL complexity. Teams comfortable with raw SQL and TypeScript benefit most; ORM users may find the SQL-first approach a learning curve.

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.