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 sqlc command-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.yml or sqlc.json configuration file (see Tips for details).

Step-by-Step

  1. 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.
    Verify installation by running: sqlc --version. If this fails, check your Go installation or project dependencies.
  2. Ensure your database is accessible from the terminal. Log in with the correct credentials (e.g., psql -h localhost -U username for PostgreSQL). If using a remote DB, ensure network/firewall rules allow connections.
  3. 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/`).
  4. Generate compiled code by running: sqlc generate. This creates Go/Python/Java/etc. code in a generated directory. Example: If you have a file queries.sql, compile it directly: sqlc generate -f queries.sql. For multiple files: sqlc generate -f src/*.sql.
  5. 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

  1. Missing SQLC config: The default behavior assumes a sqlc.yml file exists. If you omit it, sqlc will prompt for schema and query locations. To avoid this, create a config file:
    name: your_app
    schemas:
      - ./schema.sql
    queries:
      - ./queries.sql
    Fix: Run sqlc generate again with the file present.
  2. 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.
    Fix: Test the connection manually (e.g., psql -U user -h localhost).
  3. 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., SELECT on required tables).
    • Joins or joins are valid in the DB.
    Fix: Use a tool like pg_isolation (PostgreSQL) or mysqlcheck to verify data.

Tips

  1. 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
  2. Use a config file for portability: A sqlc.yml file lets you reuse the same code across DB backends. Example:
    name: api
    schemas:
      - ./db/schema.sql
    queries:
      - ./db/queries.sql
    dialect: postgres
  3. Test edge cases: sqlc handles many SQL constructs, but test:
    • Complex joins.
    • Transactions.
    • Raw SQL (e.g., EXECUTE IMMEDIATE).
  4. Avoid raw SQL unless necessary. sqlc is best for parameterized queries. For dynamic SQL, use EXECUTE IMMEDIATE with placeholders (e.g., ?).

Troubleshooting

  1. 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.sql or sqlc generate -f ./src/*.sql.
  2. 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).
    Last resort: Recreate the database and refresh your schema.
  3. 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.
    Fix: Run queries manually in the DB shell (e.g., psql) to debug.
  4. Missing dependencies: If sqlc generates Go code but go run fails, install the Go dependencies: go mod tidy.

Frequently Asked Questions

How do I use sqlc with a specific database backend?

  1. Specify the dialect in sqlc.yml:
    dialect: postgres
    For MySQL, use dialect: mysql. If omitted, sqlc defaults to SQLite.

Can I compile SQL into Python instead of Go?

  1. 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?

  1. 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?

  1. Check the full error message, which often points to syntax or dialect issues.
  2. 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)?

  1. 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?

  1. Yes, but you must configure the sqlc.yml dialect and connection details for each target DB.