Skip to main content
The Metrics SQL API is a Postgres wire protocol compatible interface for querying your Lightdash semantic layer. Any tool that can connect to a PostgreSQL database โ€” psql, BI tools, notebooks, database drivers โ€” can connect to Lightdash and query your metrics and dimensions with SQL. Your explores are exposed as tables, and their dimensions and metrics as columns. When you run a query, Lightdash compiles it through the semantic layer, so metric calculations, joins, and access controls are handled for you โ€” you get the same numbers as in the Lightdash UI.
The Metrics SQL API is currently in beta and only available on Lightdash Enterprise plans. Itโ€™s disabled by default and must be enabled for your organization by an admin.For more information on our plans, visit our pricing page.

Connect

You can find copy-paste connection details in your project settings under Semantic Layer API. Connect with any Postgres-compatible client using: As a connection URI:
Queries run with the permissions of the token, so you can only query explores and fields that the tokenโ€™s account has access to.

How queries work

  • Tables are explores. Each explore in your project appears as a table, and FROM takes a single explore. Fields from tables joined in the explore are included as columns โ€” joins are defined in the semantic layer, not in your SQL.
  • Columns are field IDs. Dimensions and metrics use their Lightdash field IDs, e.g. orders_status, orders_total_order_amount.
  • Metrics are pre-aggregated. Select a metric like any other column and Lightdash computes the aggregation for you. GROUP BY is optional โ€” results are implicitly grouped by the dimensions you select. If you do write one, itโ€™s validated against your SELECT list.
  • Results are limited to 500 rows by default. Add a LIMIT to override it.
Supported SQL includes WHERE (=, !=, <, <=, >, >=, IN, LIKE/ILIKE, BETWEEN, IS NULL, AND/OR), HAVING, ORDER BY, LIMIT, and calculated expressions in the SELECT list (arithmetic, functions, CASE, and window functions).

Examples

Discover explores and fields

The catalog is exposed through information_schema. The field_type column on information_schema.columns tells you whether a field is a dimension or a metric:

Query a metric by a dimension

Select the dimensions and metrics you want โ€” no GROUP BY or aggregate functions needed:

Filter and limit

Calculated expressions

Combine metrics and dimensions with expressions in the SELECT list, like table calculations in the Lightdash UI:

Query from Python

Because the interface speaks the Postgres wire protocol, existing Postgres drivers work:
If a query isnโ€™t valid, error messages include hints โ€” an unknown column error lists the available fields, and using an aggregate function suggests the matching metric instead.

Self-hosting

If you self-host Lightdash, the Metrics SQL API server is disabled until you set the PGWIRE_PORT environment variable. TLS is required by default, so you also need to provide a certificate and key (PEM format) for the hostname clients connect to:
  • If your certificate is from a public CA (e.g. Letโ€™s Encrypt), clients can use sslmode=require or verify-full.
  • If you use a private CA or a self-signed certificate, clients either pass the CA with sslrootcert=/path/to/ca.pem for verification, or use sslmode=require (encrypted, but the server identity isnโ€™t verified).
The server starts a separate TCP listener on that port, so youโ€™ll also need to expose it through your load balancer or network configuration alongside the main Lightdash port. It requires a valid LIGHTDASH_LICENSE_KEY โ€” see Enterprise License Keys.

Limitations

  • No explicit joins, subqueries, or CTEs. Queries select from a single explore; joins are defined in the semantic layer.
  • No DML. The API is read-only โ€” SELECT queries only.
  • No custom metrics or period-over-period comparisons.
  • Simple query protocol only. Prepared statements and bind parameters (the extended query protocol) arenโ€™t supported. psql and drivers sending plain-text queries work; some GUI tools wonโ€™t.
  • No pg_catalog emulation. Schema browsers in GUI clients (e.g. DBeaver) wonโ€™t populate their sidebar โ€” query information_schema.tables and information_schema.columns instead.
sslmode=require encrypts the connection; verify-full additionally verifies the serverโ€™s certificate and hostname, protecting against impersonation. On Lightdash Cloud the certificate is publicly trusted (Letโ€™s Encrypt), but some Postgres clients donโ€™t check the system trust store by default:
  • psql / psycopg (libpq 16+): add sslrootcert=system to use the OS trust store. On older libpq, point sslrootcert at your system CA bundle (e.g. /etc/ssl/certs/ca-certificates.crt).
  • JDBC (DBeaver, DataGrip): add sslfactory=org.postgresql.ssl.DefaultJavaSSLFactory to use Javaโ€™s built-in trust store.
  • node-postgres, Go, Rust, .NET: use the runtimeโ€™s trust store natively โ€” no extra configuration.
Validate via the CA chain as above โ€” donโ€™t pin the certificate itself, as itโ€™s rotated automatically every ~60 days.
If youโ€™re working in Python, the Lightdash Python SDK offers a typed, dataframe-native way to query the semantic layer without writing SQL.