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.
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
HumHub — Open-Source Enterprise Social Network
A flexible, open-source social networking platform built on Yii2 for creating private communities, intranets, and collaboration spaces within organizations.
Dolibarr — Open-Source ERP & CRM for Business Management
A modular open-source ERP and CRM application written in PHP for managing contacts, invoices, orders, inventory, accounting, and more from a single web interface.
PrestaShop — Open-Source PHP E-Commerce Platform
A widely adopted open-source e-commerce platform written in PHP with a rich module marketplace, multi-language support, and a strong European user base.