Skip to content

Guide · Open source

How to use sqlc: write SQL, generate type-safe code

Updated · written with AI from the project's own documentation and checked against our catalogue data

sqlc is an open-source tool that reads the SQL queries you write and generates code in your programming language, with type-safe functions for running them. This guide explains the workflow, the languages it supports, and when it fits a project. After reading it you will understand the three-step process and know where to find the install instructions.

sqlcsqlc-dev/sqlc · ★ 18.4k · MIT

Quick start

  1. 1Clone the repository. Run the git clone command from the README to get the sqlc source code.
  2. 2Read the installation docs. Follow the installation page in the sqlc documentation to set up the tool, since the README does not list prerequisites.
  3. 3Write your SQL queries. Write the queries your application needs in SQL, as you would normally.
  4. 4Run sqlc to generate code. Run sqlc so it reads your queries and produces type-safe functions in your chosen language.
  5. 5Call the generated functions. Use the generated functions from your application code instead of writing raw query strings.

What sqlc does and how the workflow works

sqlc is described as a SQL compiler. You write queries in SQL yourself, run sqlc to generate code with type-safe interfaces to those queries, and then write application code that calls the generated code.

The point is that you keep control of the SQL. sqlc does not build queries for you from method calls, so the queries you read in your code are the same ones that run against the database.

  • You write queries in SQL.
  • You run sqlc to generate code with type-safe interfaces to those queries.
  • You write application code that calls the generated code.

Install sqlc from the repository

The README's install command clones the source repository. For the full set of installation options, the project points to its documentation page on installation.

The README does not list prerequisites, so check the installation docs before you start.

git clone https://github.com/sqlc-dev/sqlc

Choose a target language

The README lists four language generators: sqlc-gen-go, sqlc-gen-kotlin, sqlc-gen-python and sqlc-gen-typescript. Pick the one that matches your application.

The README also says additional languages can be added through plugins. A community plugin list is linked from the language support reference in the documentation.

  • Go: sqlc-gen-go
  • Kotlin: sqlc-gen-kotlin
  • Python: sqlc-gen-python
  • TypeScript: sqlc-gen-typescript

Know which databases are supported

The README says sqlc works with PostgreSQL, MySQL and SQLite. The excerpt used for this guide does not show a full list of supported databases, so confirm the details in the official documentation before committing to a database engine.

Decide whether sqlc fits your project

sqlc suits Go developers who prefer plain SQL over an ORM. It also suits teams that want query mistakes caught when code is generated rather than at runtime. Projects in Python, Kotlin or TypeScript can use it through the listed generators.

It is a poor fit if you want to avoid writing SQL, because the workflow starts with hand-written queries. Projects that need a language with no generator or plugin will need to build or find one.

  • Good fit: Go teams who want plain SQL with generated, type-safe functions.
  • Good fit: teams that want errors caught at generation time.
  • Poor fit: people who want to avoid writing SQL entirely.
  • Poor fit: languages not covered by a generator or plugin.

What people use it for

  • Go services that query PostgreSQL, MySQL or SQLite and want type-safe functions instead of hand-written scanning code.
  • Python, Kotlin or TypeScript projects that want generated query code through the listed sqlc-gen plugins.
  • Teams that want SQL mistakes surfaced when code is generated rather than when the app runs.
  • Trying sqlc in the interactive playground at play.sqlc.dev before adding it to a project.

Frequently asked questions

Is sqlc free to use?

sqlc is released under the MIT licence, as stated in the project summary. Check the LICENSE file in the repository for the full terms.

What languages does sqlc support?

The README lists generators for Go, Kotlin, Python and TypeScript. Other languages can be added through community plugins described in the documentation.

Which databases does sqlc work with?

The README says it works with PostgreSQL, MySQL and SQLite. The excerpt does not show a complete list, so check the documentation for details.

Do I need to write SQL to use sqlc?

Yes. The workflow starts with SQL queries you write yourself, and sqlc generates code from them.

How is sqlc different from an ORM?

sqlc generates functions from the SQL you write rather than building queries through an object model. Comparable projects mentioned in this review include sqlx, GORM, Ent and sqlboiler.

Where can I see sqlc working before installing it?

The README links an interactive example and a playground at play.sqlc.dev, which show the workflow in the browser.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

sqlc suits developers, especially in Go, who want to keep writing plain SQL and get type-safe code generated from it. It is less suitable if you want to avoid SQL entirely or need a language with no generator or plugin, in which case an ORM or a library such as sqlx, GORM, Ent or sqlboiler may fit better.

Install options & downloads