CLI tool
adelsz/pgtyped avatar
adelsz/pgtyped

pgTyped: generated TypeScript types for raw SQL, checked against a live Postgres

pgTyped - Typesafe SQL in TypeScript

3,283 stars120 forksTypeScriptMIT

At a glance

What is it?
pgTyped reads your SQL and your running PostgreSQL schema, then emits typed query functions you import in TypeScript. It suits teams that want raw SQL and compile-time safety without a schema DSL.
Who is it for?
Adopt pgTyped if you already write raw SQL against Postgres and want the compiler to catch column and parameter mismatches without moving your schema into TypeScript. Type generation requires a reachable PostgreSQL schema; generated TypeScript can be committed and compiled separately, while executing its queries still requires a database. pgTyped is not a query builder or migration tool.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 1 day ago.
What is it written in?
Mainly TypeScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 22, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • adelsz/pgtyped3,283 ★ · -1
Star history
Repository2026-09-242026-11-22
adelsz/pgtyped3.3k3.3k

-1 Stars in the last 59 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The gap pgTyped fills between raw SQL and TypeScript

A team that writes SQL by hand in a TypeScript service gets two failure modes. A column gets renamed in a migration and the query keeps compiling until runtime. A parameter is passed as a string where the column is an integer, and the database raises the error. The usual fixes are an ORM, a query builder, or hand-written row interfaces that drift from the schema.

pgTyped takes a different route. It does not ask you to define your schema in TypeScript. The README states that it generates types and interfaces for your SQL queries by using your running Postgres database as the source of type information. That means the database itself is the authority. If the schema changes, you regenerate, and the compiler tells you which call sites broke.

The audience is narrow and specific: teams already committed to Postgres, already comfortable writing SQL, and already using TypeScript. If your queries are simple enough that a query builder covers them, pgTyped adds a code generation step you do not need. If your team has no appetite for raw SQL, the generated interfaces will not help.

How pgTyped derives types from a running database

The pipeline has three inputs: your config file, your query files, and a database connection. The README describes extracting queries from both SQL and TS files. In a SQL file, a query is marked with a comment directive, shown in the README as `/* @name FindBookById */`, and parameters are written with a colon prefix, as in `WHERE id = :bookId`.

The CLI parses those queries, sends them to Postgres for description, and writes a sibling file. The README's example turns `books.sql` into `books.queries.ts`, which contains a params interface, a result interface, and a `PreparedQuery` export. The generated result interface in the README shows `id: number`, `rank: number | null`, `name: string | null` and `author_id: number | null`. Nullability comes from the schema, not from a guess.

One design point is worth calling out. The README states pgTyped prevents SQL injections by not doing explicit parameter substitution: queries and parameters are sent separately to the driver, so the PostgreSQL server performs substitution. That is the correct choice, and it also means the generated object is a wrapper around a parameterized query rather than a string you can log and run by hand.

Query extraction from TS files is supported too, which matters for teams that keep queries next to the code that runs them. The README links separate documentation pages for SQL-file and TS-file query authoring, including parameter expansions, so the two paths are not identical in what they allow.

Installing pgTyped and running a first typed query

The README gives four steps. The CLI is a dev dependency and TypeScript is a required peer dependency, which the README states explicitly. The runtime package is the only required runtime dependency.

bash
npm install -D @pgtyped/cli typescript
npm install @pgtyped/runtime

Next you create a `config.json` file. The README does not print its contents, so check the CLI documentation page it links before filling it in; the only flag the README shows is `-c` pointing at that file.

bash
npx pgtyped -w -c config.json

The `-w` flag starts watch mode, which the README describes as generating query types as you write them. With that running, save a query in a SQL file:

sql
/* @name FindBookById */
SELECT * FROM books WHERE id = :bookId;

pgTyped writes `books.queries.ts` next to it, containing `IFindBookByIdParams`, `IFindBookByIdResult` and the `findBookById` export. Import that export and call `.run()` with a params object and a `pg` client, as the README's example does with `{ bookId: 5 }`. The result is an array of rows typed by the generated interface.

The README also points at `packages/example/README.md` for a preconfigured example. If the config format is the part you are unsure about, start there rather than guessing keys.

Where pgTyped stops being the right tool

The generation step needs a reachable Postgres instance. That is the core constraint and it has consequences. A CI job that builds TypeScript without a database service will fail at the generation step, or will type-check against stale generated files. Teams that treat the database as an external dependency unavailable during builds need a committed generated output plus a check that it is current, and the README does not describe such a check.

The README's own project-state note says the APIs might change. The release history backs that up in an uneven way: v2.3.0 landed in October 2023, v2.4.2 in February 2025, v2.4.3 in March 2025. A gap of more than a year between minor releases is not a reason to avoid the tool, but it does mean you should not expect a fast answer to a breaking-change report. The last push to the repository was on 2026-09-23, so the project is not dormant, but the release cadence and the push cadence are different things.

There is also a fit question. pgTyped generates typed query wrappers, but does not manage migrations or define your database schema. If you want a typed data-access layer where the schema lives in TypeScript and migrations are part of the same tool, pgTyped is the wrong shape. It assumes you already have a database and a migration story, and it only types the queries you write.

pgTyped compared with SafeQL and Prisma TypedSQL

The closest alternative in the search data is SafeQL, which also targets typed raw SQL in TypeScript but takes a different route: it is an ESLint plugin that checks your queries at lint time rather than a generator that emits query files. The practical difference is where the types live. With SafeQL, the SQL stays in your source and the type checking happens as a lint rule, so there is no generated artifact to commit and no separate file to keep in sync. With pgTyped, you get a real module per query file with named interfaces you can import elsewhere. Teams that dislike generated code tend to prefer the lint-rule approach; teams that want the query object to be a first-class importable value tend to prefer pgTyped.

Prisma TypedSQL appears in the same search list. It is a feature of the Prisma ecosystem, so it assumes you already use Prisma for schema and migrations and it types SQL in that context. pgTyped has no schema layer at all, which is the point: it works against a database you manage however you like. If you are already on Prisma, TypedSQL is the smaller addition; if you are not, adopting Prisma to get typed SQL is a much larger commitment than adding pgTyped.

Kysely is a different animal again, a type-safe query builder where you write TypeScript rather than SQL. pgTyped's README does not mention any of these tools, so treat the comparison as a description of approaches, not as a claim about which is faster or better.

Licence, dependencies and the cost of upgrades

pgTyped is MIT licensed, copyright 2019-present, Adel Salakh. For most teams that means the usual permissive terms apply, with the standard requirement to keep the licence notice. This is not legal advice; check your own policy if you redistribute the generated files or vendor the packages.

The dependency surface is small. The root `package.json` lists `io-ts` as a dependency and requires Node 18 or later via `engines`. The CLI and runtime are separate packages, so a production install pulls in the runtime only, while the CLI and TypeScript stay in dev dependencies.

Upgrade cost is concentrated in two places. First, the generated files: a minor release can change how a type is emitted, which shows up as a diff across every `.queries.ts` file and, occasionally, as a compile error at a call site. Second, the config format and CLI flags, which live in documentation the README links to rather than in the README itself. The README's own warning that APIs might change means you should read the release notes for v2.4.x before bumping, and pin the CLI version in CI so a fresh install does not silently move you forward.

Editorial conclusion

Adopt pgTyped if you already write raw SQL against Postgres and want the compiler to catch column and parameter mismatches without moving your schema into TypeScript. Type generation requires a reachable PostgreSQL schema; generated TypeScript can be committed and compiled separately, while executing its queries still requires a database. pgTyped is not a query builder or migration tool. Before committing, verify that a clean checkout can run the CLI against your actual database and that the generated files land where your tsconfig expects them.

Frequently asked questions

Does pgTyped need a running Postgres database to generate types?

Yes. The README states that pgTyped uses your running Postgres database as the source of type information, so the CLI needs a reachable connection when it generates query types.

How do I install pgTyped and start it in watch mode?

Install the CLI and TypeScript as dev dependencies plus the runtime package, create a config.json, then run npx pgtyped -w -c config.json. The -w flag is watch mode, which the README describes as generating query types as you write them.

Does pgTyped protect against SQL injection?

The README states that pgTyped does not do explicit parameter substitution: queries and parameters are sent separately to the database driver, so the PostgreSQL server performs the substitution.

Can pgTyped extract queries from TypeScript files as well as SQL files?

Yes. The README lists support for extracting and typing queries from both SQL and TS files, with separate documentation pages for each authoring style.

Official sources

  1. adelsz/pgtyped on GitHub
  2. License: MIT
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/adelsz-pgtyped.svg)](https://hysenlabs.com/projects/adelsz-pgtyped)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/adelsz-pgtyped.svg)](https://hysenlabs.com/projects/adelsz-pgtyped)

Was this useful?