Generate kotlinx.serialization data classes directly from a Postgres (or Supabase) database.
This is a small standalone Rust CLI that fills the gap until Kotlin type generation lands
upstream in supabase/postgres-meta#1086.
It introspects the live schema straight from pg_catalog (the same source postgres-meta uses)
and emits idiomatic @Serializable classes, so it's a drop-in you can retire once the official
generator ships.
Published on crates.io:
cargo install postgres-kotlin-genThis installs the postgres-kotlin-gen binary into ~/.cargo/bin (make sure it's on your PATH).
Verify with:
postgres-kotlin-gen --helpRequires a Rust toolchain. To install a specific version, use
cargo install postgres-kotlin-gen --version 0.1.0.
For each schema it generates one namespace object containing:
- Enums →
@Serializable enum classwith@SerialNameper label. - Composite types →
@Serializable data class. - Tables → three variants:
Table— the full row (Select).TableInsert— writable columns; columns with a default/identity or that are nullable become optional (T? = null).GENERATED ALWAYScolumns are omitted.TableUpdate— every writable column optional for partial updates.
- Views / materialized views → a single read-only
data class.
Kotlin-idiomatic details, matching the upstream PR:
- Column names are camelCased;
@SerialName("...")is added only when the name actually differs. - Nullable properties get
= nulldefaults so Insert/Update payloads stay optional. - Empty tables become a plain
@Serializable class(a data class needs at least one parameter). - Hard keywords (
class,object,when, …) are backtick-escaped. - Imports (
SerialName,JsonElement) are added only when used.
| Postgres | Kotlin |
|---|---|
int2 |
Short |
int4 / serial |
Int |
int8 / bigserial / oid |
Long |
float4 |
Float |
float8 |
Double |
numeric / decimal / money |
Double |
bool |
Boolean |
json / jsonb |
JsonElement |
uuid, text, varchar, date/time, etc. |
String |
enum type |
generated enum class |
| composite type | generated data class |
T[] |
List<T> |
| anything else | JsonElement |
# Local Supabase (postgres:postgres@127.0.0.1:54322)
postgres-kotlin-gen --local --out Database.kt
# Any connection string (hosted Supabase pooler, sslmode handled from the URL)
postgres-kotlin-gen --db-url "postgresql://postgres:pass@db.project.supabase.co:5432/postgres" \
--package com.example.db --out Database.kt
# Falls back to $SUPABASE_DB_URL then $DATABASE_URL
export SUPABASE_DB_URL=postgresql://...
postgres-kotlin-gen --schema public --schema auth| Flag | Description |
|---|---|
--db-url <URL> |
Connection string. Falls back to $SUPABASE_DB_URL, $DATABASE_URL. |
--local |
Shortcut for the default local Supabase database. |
--schema <NAME> |
Schema to include (repeatable). Defaults to public. |
--package <NAME> |
Kotlin package declaration for the file. |
--visibility <public|internal> |
Access modifier for generated declarations (default public). |
-o, --out <FILE> |
Write to a file instead of stdout. |
For local development or building without installing from crates.io:
git clone https://github.com/AndroidPoet/postgres-kotlin-gen
cd postgres-kotlin-gen
cargo build --release
./target/release/postgres-kotlin-gen --help