docs/content/product/configuration/multiple-data-sources.mdx
Cube supports connecting to multiple data sources, so that different cubes reference data from different databases.
Usually, data sources are configured statically (see below). However, Cube can also lookup data sources dynamically which is useful in complex scenarios involving multitenancy.
Declare the list of data sources using the <EnvVar>CUBEJS_DATASOURCES</EnvVar> environment variable, then use decorated environment variables to configure each data source:
CUBEJS_DATASOURCES=default,datasource1
CUBEJS_DB_TYPE=postgres
CUBEJS_DB_NAME=ecom
CUBEJS_DB_HOST=localhost
CUBEJS_DS_DATASOURCE1_DB_TYPE=postgres
CUBEJS_DS_DATASOURCE1_DB_NAME=ecom
CUBEJS_DS_DATASOURCE1_DB_HOST=remotehost
Cube expects that the default data source is always defined. Ensure that
CUBEJS_DB_* environment variables are set or that the default data
source is defined using driverFactory in your
cube.js file.
Cube allows database-specific environment variables to be decorated with a data source name:
CUBEJS_[DS_<DATASOURCE>_]<ENV_VAR>
For example, using the datasource1 data source, <EnvVar>CUBEJS_DB_TYPE</EnvVar> could be
decorated as:
CUBEJS_DS_DATASOURCE1_DB_TYPE=postgres
For more information on environment variables that support decoration, check the environment variables reference or database-specific pages.
Use the data_source
property to set a data source for each cube:
cubes:
- name: orders
# ...
data_source: default
- name: orders_from_other_data_source
# ...
data_source: other_data_source
cube(`orders`, {
// ...
data_source: `default`
})
cube(`orders_from_other_data_source`, {
// ...
data_source: `other_data_source`
})
Follow these steps to connect to multiple data sources in Cube Cloud:
default database connection when creating a new deployment.cube.js file.