> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# JDBC catalog

> The database supports JDBC catalogs from v3.0 onwards.

**Beta feature**

[Advice on use of Beta features](/documents/4.2/maturity/features.md)

The database supports JDBC catalogs from v3.0 onwards.

A JDBC catalog is a kind of external catalog that enables you to query data from data sources accessed through JDBC without ingestion.

Also, you can directly transform and load data from JDBC data sources by using [INSERT INTO](/documents/4.2/sql-reference/sql-statements/loading_unloading/INSERT.md) based on JDBC catalogs.

JDBC catalogs support MySQL and PostgreSQL from v3.0 onwards, Oracle and SQLServer since v3.2.9 and v3.3.1, and ClickHouse (Experimental) since v3.3.0.

## Prerequisites

* The FEs and BEs or CNs in your cluster can download the JDBC driver from the download URL specified by the `driver_url` parameter.
* `JAVA_HOME` in the **\$BE\_HOME/bin/start\_be.sh** file on each BE or CN node is properly configured as a path in the JDK environment instead of a path in the JRE environment. For example, you can configure `export JAVA_HOME = <JDK_absolute_path>`. You must add this configuration at the beginning of the script and restart the BE or CN for the configuration to take effect.

## Create a JDBC catalog

### Syntax

```SQL theme={null}
CREATE EXTERNAL CATALOG <catalog_name>
[COMMENT <comment>]
PROPERTIES ("key"="value", ...)
```

### Parameters

#### `catalog_name`

The name of the JDBC catalog. The naming conventions are as follows:

* The name can contain letters, digits (0-9), and underscores (\_). It must start with a letter.
* The name is case-sensitive and cannot exceed 1023 characters in length.

#### `comment`

The description of the JDBC catalog. This parameter is optional.

#### `PROPERTIES`

The properties of the JDBC Catalog. `PROPERTIES` must include the following parameters:

| **Parameter** | **Description** |
| - | - |
| type | The type of the resource. Set the value to `jdbc`. |
| user | The username that is used to connect to the target database. |
| password | The password that is used to connect to the target database. |
| jdbc\_uri | The URI that the JDBC driver uses to connect to the target database. For MySQL, the URI is in the `"jdbc:mysql://ip:port"` format. For PostgreSQL, the URI is in the `"jdbc:postgresql://ip:port/db_name"` format. For more information: [PostgreSQL](https://jdbc.postgresql.org/documentation/head/connect.html). |
| driver\_url | The download URL of the JDBC driver JAR package. An HTTP URL or file URL is supported, for example, `https://repo1.maven.org/maven2/org/postgresql/postgresql/42.3.3/postgresql-42.3.3.jar` and `file:///home/disk1/postgresql-42.3.3.jar`. **NOTE** You can also put the JDBC driver to any same path on the FE and BE or CN nodes and set `driver_url` to that path, which must be in the `file:///<path>/to/the/driver` format. |
| driver\_class | The class name of the JDBC driver. The JDBC driver class names of common database engines are as follows:<ul><li>MySQL: `com.mysql.jdbc.Driver` (MySQL v5.x and earlier) and `com.mysql.cj.jdbc.Driver` (MySQL v6.x and later)</li><li>PostgreSQL: `org.postgresql.Driver`</li><li>Oracle: `oracle.jdbc.driver.OracleDriver`</li></ul> |
| schema\_resolver | (Optional) Explicitly specifies the schema resolver to use. Valid values: `postgresql`, `mysql`, `oracle`, `sqlserver`, `clickhouse`. Use this parameter when working with non-standard JDBC drivers that cannot be auto-detected by driver class name. If not specified, the database will auto-detect the appropriate resolver based on the `driver_class` parameter. This parameter is supported from v4.2 onwards. |

#### Optional Oracle properties

When `driver_class` is set to Oracle, you can configure the following optional properties:

| **Parameter** | **Default** | **Description** |
| - | - | - |
| oracle.number.default-scale | 6 | Set it when Oracle `NUMBER` metadata does not provide explicit precision and scale. Valid range: `0` to `38`. |
| oracle.temporal.to-datetime | false | Controls Oracle `DATE`, `TIMESTAMP`, and `TIMESTAMP WITH LOCAL TIME ZONE` mapping. If it is set to `true`, these data types are mapped to the database' `DATETIME` type; otherwise, `DATE` remains `DATE`, and `TIMESTAMP` / `TIMESTAMP WITH LOCAL TIME ZONE` are mapped to `VARCHAR(64)`. |
| oracle.timestamptz.to-datetime | false | Controls Oracle `TIMESTAMP WITH TIME ZONE` mapping. If it is set to `true`, it is mapped to the database' `DATETIME` type; otherwise, it is mapped to `VARCHAR(64)`. |

#### Optional row-count cache properties

The database caches per-table row counts from JDBC sources to avoid blocking query planning. These properties let you tune the cache behavior per catalog. If not set, the global FE configuration values are used.

| **Parameter** | **Default** | **Description** |
| - | - | - |
| jdbc\_row\_count\_cache\_refresh\_sec | 600 | Background refresh interval (seconds). After this interval, the cached value is returned immediately while a reload runs asynchronously in the background. |
| jdbc\_row\_count\_cache\_expire\_sec | 1200 | Hard eviction TTL (seconds). Cache entries not accessed within this window are evicted. Must be greater than `jdbc_row_count_cache_refresh_sec`. |
| jdbc\_row\_count\_cache\_max\_size | 10000 | Maximum number of table entries in the row-count cache for this catalog. |

> **NOTE**
>
> The FEs download the JDBC driver JAR package at the time of JDBC catalog creation, and the BEs or CNs download the JDBC driver JAR package at the time of the first query. The amount of time taken for the download varies depending on network conditions.

### Examples

The following example creates five different JDBC catalogs.

```SQL theme={null}
-- PostgresSQL
CREATE EXTERNAL CATALOG jdbc0
PROPERTIES
(
    "type"="jdbc", 
    "user"="postgres",
    "password"="changeme",
    "jdbc_uri"="jdbc:postgresql://127.0.0.1:5432/jdbc_test",
    "driver_url"="https://repo1.maven.org/maven2/org/postgresql/postgresql/42.3.3/postgresql-42.3.3.jar",
    "driver_class"="org.postgresql.Driver"
);
-- MySQL
CREATE EXTERNAL CATALOG jdbc1
PROPERTIES
(
    "type"="jdbc",
    "user"="root",
    "password"="changeme",
    "jdbc_uri"="jdbc:mysql://127.0.0.1:3306",
    "driver_url"="https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.28/mysql-connector-java-8.0.28.jar",
    "driver_class"="com.mysql.cj.jdbc.Driver"
);
-- Oracle
CREATE EXTERNAL CATALOG jdbc2
PROPERTIES
(
    "type"="jdbc",
    "user"="root",
    "password"="changeme",
    "jdbc_uri"="jdbc:oracle:thin:@127.0.0.1:1521:ORCL",
    "driver_url"="https://repo1.maven.org/maven2/com/oracle/database/jdbc/ojdbc10/19.18.0.0/ojdbc10-19.18.0.0.jar",
    "driver_class"="oracle.jdbc.driver.OracleDriver"
);
-- Oracle (with Oracle-specific optional properties)
CREATE EXTERNAL CATALOG jdbc2_ext
PROPERTIES
(
    "type"="jdbc",
    "user"="root",
    "password"="changeme",
    "jdbc_uri"="jdbc:oracle:thin:@127.0.0.1:1521/ORCLPDB1",
    "driver_url"="https://repo1.maven.org/maven2/com/oracle/database/jdbc/ojdbc10/19.18.0.0/ojdbc10-19.18.0.0.jar",
    "driver_class"="oracle.jdbc.driver.OracleDriver",
    "oracle.number.default-scale"="6",
    "oracle.temporal.to-datetime"="true",
    "oracle.timestamptz.to-datetime"="true"
);
-- SQL Server
CREATE EXTERNAL CATALOG jdbc3
PROPERTIES
(
    "type"="jdbc",
    "user"="root",
    "password"="changeme",
    "jdbc_uri"="jdbc:sqlserver://127.0.0.1:1433;databaseName=MyDatabase;",
    "driver_url"="https://repo1.maven.org/maven2/com/microsoft/sqlserver/mssql-jdbc/12.4.2.jre11/mssql-jdbc-12.4.2.jre11.jar",
    "driver_class"="com.microsoft.sqlserver.jdbc.SQLServerDriver"
);
-- ClickHouse
CREATE EXTERNAL CATALOG jdbc4
PROPERTIES
(
    "type"="jdbc",
    "user"="default",
    "jdbc_uri"="jdbc:clickhouse://127.0.0.1:8443",
    "driver_url"="https://repo1.maven.org/maven2/com/clickhouse/clickhouse-jdbc/0.4.6/clickhouse-jdbc-0.4.6.jar",
    "driver_class"="com.clickhouse.jdbc.ClickHouseDriver"
);
-- Using schema_resolver for non-standard driver
CREATE EXTERNAL CATALOG jdbc5
PROPERTIES
(
    "type"="jdbc",
    "user"="postgres",
    "password"="changeme",
    "jdbc_uri"="jdbc:postgresql://127.0.0.1:5432/mydb",
    "driver_url"="file:///path/to/custom-postgresql-driver.jar",
    "driver_class"="com.custom.PostgresDriver",
    "schema_resolver"="postgresql"
);
```

## View JDBC catalogs

You can use [SHOW CATALOGS](/documents/4.2/sql-reference/sql-statements/Catalog/SHOW_CATALOGS.md) to query all catalogs in the current cluster:

```SQL theme={null}
SHOW CATALOGS;
```

You can also use [SHOW CREATE CATALOG](/documents/4.2/sql-reference/sql-statements/Catalog/SHOW_CREATE_CATALOG.md) to query the creation statement of an external catalog. The following example queries the creation statement of a JDBC catalog named `jdbc0`:

```SQL theme={null}
SHOW CREATE CATALOG jdbc0;
```

## Drop a JDBC catalog

You can use [DROP CATALOG](/documents/4.2/sql-reference/sql-statements/Catalog/DROP_CATALOG.md) to drop a JDBC catalog.

The following example drops a JDBC catalog named `jdbc0`:

```SQL theme={null}
DROP Catalog jdbc0;
```

## Query a table in a JDBC catalog

1. Use [SHOW DATABASES](/documents/4.2/sql-reference/sql-statements/Database/SHOW_DATABASES.md) to view the databases in your JDBC-compatible cluster:

   ```SQL theme={null}
   SHOW DATABASES FROM <catalog_name>;
   ```

2. Use [SET CATALOG](/documents/4.2/sql-reference/sql-statements/Catalog/SET_CATALOG.md) to switch to the destination catalog in the current session:

   ```SQL theme={null}
   SET CATALOG <catalog_name>;
   ```

   Then, use [USE](/documents/4.2/sql-reference/sql-statements/Database/USE.md) to specify the active database in the current session:

   ```SQL theme={null}
   USE <db_name>;
   ```

   Or, you can use [USE](/documents/4.2/sql-reference/sql-statements/Database/USE.md) to directly specify the active database in the destination catalog:

   ```SQL theme={null}
   USE <catalog_name>.<db_name>;
   ```

3. Use [SELECT](/documents/4.2/sql-reference/sql-statements/table_bucket_part_index/SELECT/SELECT.md) to query the destination table in the specified database:

   ```SQL theme={null}
   SELECT * FROM <table_name>;
   ```

## Join pushdown

From v4.2 onwards, the system can push multi-table joins (both `INNER JOIN` and `CROSS JOIN`) down to the external database when all joined tables come from the same JDBC catalog. This lets the source database execute the join using its own indexes and optimizer, and returns only the joined result back to the system.

### Configure join pushdown

Join pushdown is enabled by default. Set the session variable `enable_jdbc_join_push_down` to `false` to disable it:

```SQL theme={null}
SET enable_jdbc_join_push_down = false;
```

### How it works

When the rule fires, the optimizer rewrites the local join over multiple JDBC scans into a single JDBC scan that issues one SQL query containing a JOIN against the remote database.

For example, the following query against two MySQL tables in the same JDBC catalog:

```SQL theme={null}
SELECT o.order_id, c.region, o.amount
FROM jdbc0.db.orders o
JOIN jdbc0.db.customers c ON o.customer_id = c.customer_id
WHERE o.amount >= 20 AND c.region IN ('east', 'west');
```

When `enable_jdbc_join_push_down` is `false`, the system issues two separate scans and joins the result locally. When the variable is `true`, the whole join subtree is pushed down to the remote database. The local plan contains only one JDBC scan node, and the pushed-down SQL looks similar to:

```SQL theme={null}
SELECT `c1`, `c2`, `c3` FROM (
    SELECT t0.`order_id` AS c1, t1.`region` AS c2, t0.`amount` AS c3
    FROM (SELECT `order_id`, `customer_id`, `amount` FROM `db`.`orders` WHERE `amount` >= 20) t0
    INNER JOIN (SELECT `region`, `customer_id` FROM `db`.`customers` WHERE `region` IN ('east', 'west')) t1
      ON t0.`customer_id` = t1.`customer_id`
) sr_merged;
```

Use `EXPLAIN` on a query to verify whether the join was pushed down.

### Scope and limitations

* **Join type**: `INNER JOIN` and `CROSS JOIN` are eligible for pushdown. Outer, semi, and anti joins fall back to local execution.
* **Same catalog**: all tables in a merged group must belong to the same JDBC catalog (same JDBC URI, credentials, and driver). Joins that mix two JDBC catalogs, or that mix JDBC tables with native tables, run locally.
* **Cross-table predicate required**: a group is pushed down only if it has at least one predicate that joins two tables in the group. Otherwise the rule skips it to avoid sending a pure cartesian product to the remote database.
* **All-or-nothing predicate pushdown**: every predicate owned by a group must be fully translatable to the remote dialect. If any predicate cannot be pushed (for example, a StarRocks-only function), the whole group falls back to a local join.
* **Statistics**: the merged JDBC scan reuses the same default statistics as a single-table JDBC scan. Downstream operators see the joined result as if it were one table of default size, and cardinality is not estimated from the underlying tables.

## Query JDBC data with native SQL

From v4.1 onwards, the database supports querying JDBC data with database-native `SELECT` statements by using the [`native_query`](/documents/4.2/sql-reference/sql-functions/table-functions/native_query) table function.

`native_query` is useful when the source database must run SQL that cannot be expressed as a single external table query, such as source-side joins, pre-filtered subqueries, or vendor-specific SQL syntax. The database exposes the pass-through query result as a normal relation, so you can continue to apply StarRocks-side filters, joins, aggregations, and projections.

For syntax, limitations, and examples, see [`native_query`](/documents/4.2/sql-reference/sql-functions/table-functions/native_query).

## FAQ

What do I do if an error suggesting "Malformed database URL, failed to parse the main URL sections" is thrown?

If you encounter such an error, the URI that you passed in `jdbc_uri` is invalid. Check the URI that you pass and make sure it is valid. For more information, see the parameter descriptions in the "[PROPERTIES](#properties)" section of this topic.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.