r/java • u/nikita-volkov • Apr 07 '26
pGenie: open-source SQL-first PostgreSQL codegen for Java
Hey r/java!
Do the following pain points resonate with you?
- ORMs hide SQL and make performance tuning a nightmare
- Query builders force you to maintain model classes and fight schema drift
- Hand-written JDBC/RowMapper code silently breaks when the DB schema changes
While addressing them I've built pGenie – a true SQL-first code generator that treats your PostgreSQL database as the single source of truth. It's an open-source tool that takes your PostgreSQL migration files and parameterized SQL query files and generates a complete, ready-to-use Maven client library targeting Java and pgJDBC. No ORM, no DSL, no annotation processors, no reflection — just plain SQL in, type-safe Java code out.
What gets generated:
Each SQL query file becomes a Java record class modeling the parameters to the query. That record comes with an associated record type modeling the result row. You pass parameters via the constructor, call .execute(conn) with a JDBC Connection, and get back a typed result. That's it.
-- queries/insert_album.sql
insert into album (name, released, format, recording)
values ($name, $released, $format, $recording)
returning id
Becomes:
// Generated
InsertAlbum.Result result ;
System.out.;
PostgreSQL enums → Java enum with EnumCodec. Composite types → Java record. Arrays → List<T>. Nullable columns → boxed types or Optional<T> (configurable). Even exotic types like ranges and multiranges are fully supported.
The generated project is not a black box, you get:
pom.xmlwith runtime dependencies declared- One
.javafile per statement, fully readable - Integration tests per statement using Testcontainers — spin up a real PostgreSQL container, run your migrations, execute each statement
mvn verifyjust works
What else pGenie does:
- Validates migrations against a real PostgreSQL schema in CI — no more "works locally, breaks in prod"
- Manages indexes declared in your migration files automatically
- Generates Rust and Haskell bindings from the same SQL source if you need multi-language SDKs
- Builds reproducibly: a lockfile pins the codegen version
- Extends via a decentralized plugin system: write your own code generators in Dhall and plug them in via URL — no need to approve anything with the core team
Demo:
The demo repo has complete working SQL migrations and queries. The generated Java library is in artifacts/java/. You can inspect it directly or regenerate it from scratch with pgn generate.
→ pgenie.io | docs | demo | java.gen plugin
View all comments
Show parent comment
1
u/nikita-volkov Apr 07 '26
Thanks! You're right. It is in the same realm of tools.
Regarding batch insert, I've intentionally focused the generated code on the most universal API surface, which lets you either execute the statement on a JDBC
Connectionor bind aPreparedStatementand read from aResultSet.This leaves it up to the user to decide how they want to manage their connections and statements, and how they want to handle batching.
However I hear you. The
jdbcbridge library that I've referred to will likely grow in funcionality over time and include features like pooling and batch insert out of the box.