DEV.co
Open-Source Databases · cornucopia-rs

cornucopia

Cornucopia is a Rust code generator that creates type-safe database interfaces from PostgreSQL queries at compile time. It validates queries against a live database and produces a separate Rust crate with strongly-typed wrappers, eliminating SQL errors before deployment.

Source: GitHub — github.com/cornucopia-rs/cornucopia
1.1k
GitHub stars
71
Forks
Rust
Primary language
Apache-2.0
License (OSI-approved)

Key facts

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

FieldValue
Repositorycornucopia-rs/cornucopia
Ownercornucopia-rs
Primary languageRust
LicenseApache-2.0 — OSI-approved
Stars1.1k
Forks71
Open issues28
Latest releasecornucopia-v1.0.0 (2026-05-20)
Last updated2026-06-25
Sourcehttps://github.com/cornucopia-rs/cornucopia

What cornucopia is

Cornucopia performs compile-time validation of raw SQL queries against a PostgreSQL instance, then generates type-checked Rust bindings with support for custom types, arrays, and enums. It produces native Rust code compatible with sync/async patterns and connection pools, with performance parity to hand-written rust-postgres.

Quickstart

Get the cornucopia source

Clone the repository and explore it locally.

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

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

Best use cases

Type-Safe SQL in Large Rust Codebases

Projects with complex SQL that need compile-time guarantees. Catch schema mismatches and type errors before runtime, particularly valuable in teams where SQL and Rust code evolve together.

PostgreSQL-Native Applications

Systems leveraging PostgreSQL-specific features (custom types, arrays, enums). Cornucopia maps these directly to Rust without ORM abstraction overhead.

Performance-Critical Database Layers

Applications where query performance matters and ORM overhead is unacceptable. Generated code stays close to raw rust-postgres performance.

Implementation considerations

  • Requires a live PostgreSQL database accessible during code generation; plan CI/CD pipeline accordingly to provision or mock the database.
  • Generated code lives in a separate crate that must be kept in sync with SQL definitions; establish a workflow for regenerating when queries change.
  • Version 1.0 merged significant codegen rewrites from the Clorinde fork; upgrading from 0.9.x requires migration. Review the migration guide and test thoroughly.
  • SQL annotations (comments like `--! query_name`) are mandatory; ensure your SQL files follow the exact syntax or code generation will fail.
  • Custom type mappings must be configured in the tool; standard types work out-of-the-box, but domain-specific types need explicit Rust struct definitions.

When to avoid it — and what to weigh

  • Non-PostgreSQL Databases Required — Cornucopia is PostgreSQL-only. If you need MySQL, SQLite, or other RDBMS support, this tool will not fit.
  • Schema-First or Dynamic Query Patterns — The tool requires static SQL queries known at code-generation time. Highly dynamic query construction or schema discovery patterns are unsupported.
  • Live Database Availability During Build — Cornucopia must connect to a PostgreSQL instance during the code generation phase. CI/CD pipelines without database access will require workarounds.
  • Minimal Overhead / Constraint on Dependencies — Adding a generated crate and its dependencies increases build complexity and binary size. If you need minimal deps, consider lightweight query builders instead.

License & commercial use

Licensed under Apache-2.0 and MIT dual licensing. Both are permissive OSI-approved licenses with clear terms.

Commercial use is permitted under both Apache-2.0 and MIT. No restrictions on proprietary or closed-source projects using the generated code. Contributions must follow dual-license terms per the Apache-2.0 CLA.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityModerate
DEV.co fitGood
Assessment confidenceHigh
Security considerations

Cornucopia itself does not execute SQL; it parses and validates against a database. The generated code uses rust-postgres, which handles parameterized queries and prepared statements. Security depends on (1) using the tool correctly (parameterized queries, not string interpolation), (2) the security posture of the PostgreSQL instance used during code generation, and (3) proper management of database credentials in CI/CD. No known CVEs noted in provided data, but a security audit was not performed for this review.

Alternatives to consider

sqlc (Go)

Language-agnostic SQL-to-code generator with broader database support (PostgreSQL, MySQL, SQLite). Better if you use multiple languages or need non-Postgres databases.

Diesel (Rust ORM)

Rust-native, type-safe, with query builder DSL and migration system. Choose if you prefer ORM abstraction and don't need raw SQL control.

sqlx (Rust async/sync)

Lightweight, runtime-checked (not compile-time), works with async/sync. Good fit if you want less build-time complexity and can tolerate runtime validation.

Software development agency

Build on cornucopia with DEV.co software developers

Cornucopia bridges SQL and Rust type systems. Start with the book and examples, then plan your code-generation workflow. For guidance on integration into your CI/CD and schema evolution strategy, consult a Rust specialist.

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.

cornucopia FAQ

Can I use Cornucopia with MySQL or SQLite?
No. Cornucopia is PostgreSQL-only by design. It leverages PostgreSQL's query preparation and type introspection APIs, which are not portable to other databases.
Do I need to install PostgreSQL to use generated Cornucopia code?
No. The live database is only required during code generation (build time). At runtime, you connect to any PostgreSQL instance as normal. Generated code has no special dependencies on the dev database.
What happens if my database schema changes?
You must re-run Cornucopia to regenerate the Rust code. The tool will fail if your SQL queries no longer match the schema, alerting you to breaking changes before they reach production.
Is the generated code readable and modifiable?
The generated code is intended to be used as-is (as a separate crate dependency). Modifying it directly is not recommended because re-generation will overwrite changes. If customization is needed, wrap the generated code in your own types.

Custom software development services

DEV.co is a software development agency delivering custom software development services to companies building on open source. Our software developers and web developers design, integrate, and ship production systems — spanning web development, APIs, AI, data, and cloud. If cornucopia is part of your open-source databases roadmap, our team can implement, customize, migrate, and maintain it.

Ready to add compile-time SQL safety to your Rust project?

Cornucopia bridges SQL and Rust type systems. Start with the book and examples, then plan your code-generation workflow. For guidance on integration into your CI/CD and schema evolution strategy, consult a Rust specialist.