sqlc — Generate Type-Safe Go Code from SQL
sqlc generates fully type-safe Go code from your SQL queries. Write SQL, run sqlc generate, and get Go functions with proper types for parameters and results — no ORM, no reflection, just compile-time safe database access.
Ready-to-run agent install
This asset can be installed after the agent chooses its runtime, checks the plan, and runs the matching command.
npx -y tokrepo@latest install 10d1b389-373d-11f1-9bc6-00163e2b0d79 --target codexRun after dry-run confirms the install plan.
What it is
sqlc is a code generation tool that reads your SQL schema and query files, validates them, and produces type-safe Go functions. Instead of using an ORM or embedding SQL strings with manual type assertions, you write plain SQL and let sqlc generate the Go structs and methods. The generated code has zero runtime reflection and catches type errors at compile time.
sqlc is built for Go developers who prefer writing raw SQL over ORM abstractions. It supports PostgreSQL, MySQL, and SQLite, and integrates into standard Go build pipelines.
How it saves time or tokens
sqlc eliminates the boilerplate of hand-writing Go structs for query results and scanning rows into typed variables. A single sqlc generate command produces all the data access code from your SQL files. Schema changes are caught at generation time rather than at runtime, reducing debugging cycles. The generated code is straightforward and readable, so there is no ORM magic to debug.
How to use
- Install sqlc:
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latestorbrew install sqlc. - Create a
sqlc.yamlconfig file and write your SQL schema and query files with sqlc annotations like-- name: GetUser :one. - Run
sqlc generateto produce Go source files with typed functions for each query.
Example
-- schema.sql
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL
);
-- query.sql
-- name: GetUser :one
SELECT id, name, email FROM users WHERE id = $1;
-- name: ListUsers :many
SELECT id, name, email FROM users ORDER BY name;
-- name: CreateUser :one
INSERT INTO users (name, email) VALUES ($1, $2) RETURNING *;
After running sqlc generate, you get Go functions like GetUser(ctx, id) returning a typed User struct.
Related on TokRepo
- AI Tools for Database -- explore AI-powered database tools and query assistants
- AI Tools for Coding -- discover coding workflows that complement SQL-first development
Common pitfalls
- sqlc validates queries against your schema at generation time, so your schema file must be kept in sync with your actual database.
- Complex dynamic queries with conditional WHERE clauses are harder to express in sqlc; consider using sqlc for standard CRUD and a query builder for dynamic filters.
- sqlc's MySQL support is less mature than PostgreSQL; some MySQL-specific syntax may not parse correctly.
Frequently Asked Questions
sqlc generates code from SQL you write, giving you full control over queries with compile-time type safety. ORMs like GORM generate SQL from Go structs, which can produce unexpected queries. sqlc has zero runtime reflection, while ORMs use reflection heavily.
sqlc supports PostgreSQL (the most mature), MySQL, and SQLite. Each engine has its own SQL parser that validates your queries against the schema.
sqlc reads your schema files but does not run migrations itself. You use a separate migration tool like golang-migrate or Atlas and point sqlc at the resulting schema.
Yes. The generated code accepts a database interface that works with both *sql.DB and *sql.Tx, so you wrap calls in a transaction using standard Go database/sql patterns.
Yes. sqlc scales well because each query generates a single function. Large projects organize queries into multiple SQL files and use sqlc's package configuration to split generated code across Go packages.
Citations (3)
- sqlc GitHub— sqlc generates type-safe Go code from SQL with no ORM
- sqlc Documentation— sqlc supports PostgreSQL, MySQL, and SQLite
- Go Standard Library— Go database/sql package for transaction handling
Related on TokRepo
Discussion
Related Assets
Ant Design — Enterprise-Class React UI Library
Ant Design is an enterprise-class UI design language and React component library created by Alibaba. 60+ production-ready components with comprehensive patterns, internationalization for 30+ languages, and a rich theme system. The go-to choice for admin dashboards.
D2 — Declarative Diagram Scripting Language
A modern diagram scripting language that turns text into diagrams, offering a readable syntax for architecture, flow, and sequence diagrams rendered from code.
Turbopuffer — Serverless Vector DB for AI Search
Serverless vector database built for AI search at scale. Turbopuffer offers sub-millisecond queries, automatic scaling, and pay-per-query pricing with zero infrastructure.
Pomerium — Identity-Aware Zero Trust Access Proxy
Pomerium is an open source reverse proxy that provides secure, identity-aware access to internal applications without a VPN, implementing BeyondCorp-style zero trust networking with SSO integration.