SQL Language Server
sql-language-server is an implementation of the Language Server Protocol (LSP) providing intelligent features for various SQL dialects, including linting, autocompletion, and schema introspection. It is designed to be run as a standalone server process, enabling integration with code editors and IDEs (like VS Code) that support LSP. The current stable version is 1.7.1, with minor releases appearing to follow a monthly or bi-monthly cadence, indicating active development. Key differentiators include its pluggable architecture for different SQL database clients (e.g., PostgreSQL, SQLite, BigQuery) and its focus on robust parsing and error handling, making it a valuable tool for SQL development workflows. It ships with TypeScript types, facilitating its use in TypeScript-based environments.
Common errors
-
Error: Column 'column_name' not found in table 'table_name'
cause The language server's internal schema cache is outdated or incorrect for the current database connection or SQL dialect being used.fixEnsure the `sql-language-server` is correctly configured with the database connection details (e.g., PostgreSQL, SQLite). Manually trigger a schema refresh or restart the language server if the database schema has recently changed. -
Failed to resolve root path config
cause The language server could not correctly determine its root directory or configuration files, often due to an incorrect working directory or misconfigured client-side settings.fixEnsure the language server is launched from the correct working directory that contains your SQL project. Verify client-side LSP configurations that specify the `rootPath` or `workspaceFolders` are pointing to valid directories. -
Syntax error near 'IS NOT NULL' operation.
cause Older versions of the parser had a bug in correctly handling the right-hand side of `IS NOT NULL` operations.fixUpgrade to `sql-language-server` version 1.6.0 or newer, which contains a fix for this specific parsing error. If upgrading isn't an option, rephrase the SQL query to avoid the problematic `IS NOT NULL` pattern if possible.
Warnings
- breaking Upgrading `vscode-languageserver` to version `8.0.0-next.8` in v1.2.1 may introduce breaking changes or require compatibility adjustments for host environments or custom LSP clients relying on older `vscode-languageserver` API contracts.
- gotcha Prior to v1.3.0, the server could be inadvertently booted twice in certain integration scenarios, leading to unexpected behavior or resource consumption issues.
- gotcha Older versions (pre-1.2.1) had issues correctly parsing SQL identifiers enclosed in double quotes, leading to incorrect linting or autocompletion.
- gotcha Version 1.3.3 introduced a change to 'not throw an error when it's failed to parse'. While this improves resilience, it might mask underlying syntax issues if clients rely on explicit parse errors for strict validation.
Install
-
npm install sql-language-server -
yarn add sql-language-server -
pnpm add sql-language-server
Imports
- Config
import type { Config } from 'sql-language-server' - SQLOption
import type { SQLOption } from 'sql-language-server' - LSP_SERVER_NAME
import { LSP_SERVER_NAME } from 'sql-language-server'
Quickstart
npm install -g sql-language-server
# To run the language server, typically it's invoked by an LSP-enabled editor
# or manually via standard I/O (stdio) for debugging/testing:
sql-language-server --stdio
# Example of how an LSP client (e.g., in a VS Code extension) might start it:
// In a client-side extension's activate() function:
// import * as path from 'path';
// import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node';
// let serverModule = context.asAbsolutePath(path.join('node_modules', 'sql-language-server', 'lib', 'main.js'));
// let debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] };
// let serverOptions: ServerOptions = {
// run: { module: serverModule, transport: TransportKind.stdio },
// debug: { module: serverModule, transport: TransportKind.stdio, options: debugOptions }
// };
// let clientOptions: LanguageClientOptions = {
// documentSelector: [{ scheme: 'file', language: 'sql' }],
// synchronize: { fileEvents: workspace.createFileSystemWatcher('**/.sql') }
// };
// let client = new LanguageClient('sqlLanguageServer', 'SQL Language Server', serverOptions, clientOptions);
// client.start();