Yes, you can compile SQL scripts from the command line instantly—no GUI needed. Simply open a terminal, run `sqlc`, and pass your files. This tool converts SQL into executable code in a few seconds, handling database schemas and queries efficiently. No additional setup beyond installing it once.
What You'll Need
- A terminal application (Windows Command Prompt, Linux/macOS Terminal, or WSL on Windows).
- The
sqlccommand-line tool installed on your system. It’s the fastest way to compile SQL into executable programs. - At least one SQL file (e.g., queries, migrations, or schemas) to compile. Multiple files can be specified via command-line arguments.
- A database backend (MySQL, PostgreSQL, SQLite, etc.) where your SQL files target. You must ensure the database is accessible and credentials are configured.
- Optionally, a
sqlc.ymlorsqlc.jsonconfiguration file (see Tips for details).
Step-by-Step
-
Install sqlc if not already installed. Follow the official instructions for your platform:
- Linux/macOS: Run
go install github.com/sqlc-dev/sqlc/v1/cmd/sqlc@latest(requires Go 1.16+). - Windows: Use the Go installer or WSL.
sqlc --version. If this fails, check your Go installation or project dependencies. - Linux/macOS: Run
-
Ensure your database is accessible from the terminal. Log in with the correct credentials (e.g.,
psql -h localhost -U usernamefor PostgreSQL). If using a remote DB, ensure network/firewall rules allow connections. -
Write or gather your SQL files. These could be:
- Query files (e.g., `queries.sql`), each defining one function (e.g.,
GetUser). - Schema files (e.g., `schema.sql`) for migrations.
- Multiple files in a directory (e.g., `src/`).
- Query files (e.g., `queries.sql`), each defining one function (e.g.,
-
Generate compiled code by running:
sqlc generate. This creates Go/Python/Java/etc. code in agenerateddirectory. Example: If you have a filequeries.sql, compile it directly:sqlc generate -f queries.sql. For multiple files:sqlc generate -f src/*.sql. -
Test the generated code by running the compiled functions. For example:
go run generated/queries.go(assuming Go output). If using Python:python generated/queries.py.
Common Mistakes
-
Missing SQLC config: The default behavior assumes a
sqlc.ymlfile exists. If you omit it, sqlc will prompt for schema and query locations. To avoid this, create a config file:
Fix: Runname: your_app schemas: - ./schema.sql queries: - ./queries.sqlsqlc generateagain with the file present. -
Database connection issues: sqlc requires a connected DB. If you get errors like
database connection refused, verify:- Credentials are correct.
- Network access is allowed (e.g., localhost or VPN for remote DBs).
- The DB server is running.
psql -U user -h localhost). -
Incorrect SQL syntax: sqlc does not validate SQL—it only compiles the output. If your queries fail at runtime, check:
- Table/column names match the DB schema.
- Permissions are granted (e.g.,
SELECTon required tables). - Joins or joins are valid in the DB.
pg_isolation(PostgreSQL) ormysqlcheckto verify data.
Tips
-
Automate builds: Add sqlc to your CI/CD pipeline (e.g., GitHub Actions):
steps: - uses: actions/checkout@v4 - run: go install github.com/sqlc-dev/sqlc/v1/cmd/sqlc@latest - run: sqlc generate -
Use a config file for portability: A
sqlc.ymlfile lets you reuse the same code across DB backends. Example:name: api schemas: - ./db/schema.sql queries: - ./db/queries.sql dialect: postgres -
Test edge cases: sqlc handles many SQL constructs, but test:
- Complex joins.
- Transactions.
- Raw SQL (e.g.,
EXECUTE IMMEDIATE).
-
Avoid raw SQL unless necessary. sqlc is best for parameterized queries. For dynamic SQL, use
EXECUTE IMMEDIATEwith placeholders (e.g.,?).
Troubleshooting
-
sqlc fails with "no such file or directory": Check if the file path is correct. On Windows, ensure slashes are backslashes or use forward slashes:
sqlc generate -f C:\path\to\queries.sqlorsqlc generate -f ./src/*.sql. -
Database errors persist: Restart the terminal session and re-connect to the DB. If the issue is persistent, check:
- The DB logs for errors.
- Firewall rules (e.g., port 5432 for PostgreSQL).
-
Compiled code fails at runtime: The SQL is correct, but the DB state doesn’t match. Verify:
- Your queries reference the right tables.
- Data exists where expected.
- Transaction isolation levels are appropriate.
psql) to debug. -
Missing dependencies: If sqlc generates Go code but
go runfails, install the Go dependencies:go mod tidy.
Frequently Asked Questions
How do I use sqlc with a specific database backend?
- Specify the dialect in
sqlc.yml:
For MySQL, usedialect: postgresdialect: mysql. If omitted, sqlc defaults to SQLite.
Can I compile SQL into Python instead of Go?
- Yes. Install the Python package:
pip install sqlc. Then run:sqlc generate -package=queries -main=main.py.
What if my SQL queries reference variables or parameters?
- Use placeholders (e.g.,
?for PostgreSQL/MySQL) or named parameters (e.g.,{param}). sqlc will generate type-safe bindings.
How do I debug SQLC errors?
- Check the full error message, which often points to syntax or dialect issues.
- Test the SQL in a separate DB client (e.g., DBeaver, pgAdmin) to isolate the problem.
Does sqlc work with cloud databases (e.g., AWS RDS)?
- Yes, as long as your terminal can connect to the cloud endpoint. Ensure:
- IAM permissions allow access.
- Network security groups allow inbound connections (e.g., port 5432).
Can I reuse the same queries across multiple databases?
- Yes, but you must configure the
sqlc.ymldialect and connection details for each target DB.