Introspect a Postgres schema and generate typed structs/classes in the language of your choice.
npm install -g pg2struct
# or run without installing:
npx pg2struct --helppg2struct --conn <connection-string> --lang <language> [options]| Flag | Short | Description |
|---|---|---|
--conn |
-c |
Postgres connection string. Falls back to $DATABASE_URL if omitted. |
--schema |
-s |
Schema to introspect. Default: public. |
--table |
-t |
Restrict to specific table(s). Repeatable (-t a -t b) or comma-separated (-t a,b). |
--lang |
-l |
Target language for generated output (required). See below for supported values. |
--out |
-o |
Output file path. Default: stdout. |
--help |
-h |
Show usage. |
Each table becomes a named top-level type in the output (e.g. table users → type Users). Nullable columns are generated as optional/nullable fields in the target language.
pg2struct -c postgres://localhost/mydb -l go
pg2struct -c postgres://localhost/mydb -l typescript -o schema.ts
pg2struct -c postgres://localhost/mydb -l python -s app -t users -t orders
DATABASE_URL=postgres://localhost/mydb pg2struct -l rust -o schema.rsAny language supported by quicktype-core, including go, python, typescript, java, csharp, rust, kotlin, swift, ruby, dart, php, crystal, elm, haskell, objective-c, cplusplus, pike, scala3, and more. Run pg2struct with a missing or invalid --lang to print the full current list.
bun install
bun bin/pg2struct.js --helpMIT — see LICENSE.