11---
2- title : Multiple Data Sources
2+ title : Multiple data sources
33permalink : /config/multiple-data-sources
44category : Configuration
55subCategory : Advanced
66menuOrder : 2
77---
88
9- Cube supports using multiple data sources, allowing the use of different
10- databases for different cubes .
9+ Cube supports connecting to multiple data sources, so that different
10+ [ cubes ] ( /schema/reference/cube ) reference data from different databases .
1111
12- <InfoBox >
13-
14- Cube expects that a ` default ` data source is ** always** defined. Ensure that a
15- ` default ` data source is defined using [ ` driverFactory ` ]
16- [ ref-conf-ref-driverfactory] in your ` cube.js ` file ** or** that ` CUBEJS_DB_* `
17- variables exist as environment variables.
18-
19- </InfoBox >
20-
21- [ ref-conf-ref-driverfactory ] : /config#options-reference-driver-factory
22-
23- ## Schema
24-
25- To configure multiple data sources, first explicitly declare a data source in
26- some cubes by adding a ` dataSource ` property:
27-
28- ``` javascript
29- cube (' Orders' , {
30- ... ,
12+ Usually, data sources are configured ** statically** (see below). However, Cube
13+ can also lookup data sources ** dynamically** which is useful in complex
14+ scenarios involving [ multitenancy] [ ref-config-multitenancy ] .
3115
32- dataSource: ' default' ,
33- });
16+ ## Environment variables
3417
35- cube (' OrdersFromOtherDataSource' , {
36- ... ,
37-
38- dataSource: ' datasource1' ,
39- });
40- ```
41-
42- ## Configuring data sources with environment variables
43-
44- Next, Cube must be configured so that it knows which database to use for each
45- data source; first declare the data sources using the ` CUBEJS_DATASOURCES `
46- environment variable, then use
47- [ decorated environment variables] ( #environment-variables-decorated-environment-variables )
48- to configure each data source:
18+ Declare the list of data sources using the ` CUBEJS_DATASOURCES ` environment
19+ variable, then use
20+ [ decorated environment variables] ( #decorated-environment-variables ) to configure
21+ each data source:
4922
5023``` dotenv
5124CUBEJS_DATASOURCES=default,datasource1
@@ -57,6 +30,15 @@ CUBEJS_DS_DATASOURCE1_DB_NAME=ecom
5730CUBEJS_DS_DATASOURCE1_DB_HOST=remotehost
5831```
5932
33+ <InfoBox >
34+
35+ Cube expects that the ` default ` data source is ** always** defined. Ensure that
36+ ` CUBEJS_DB_* ` environment variables are set ** or** that the ` default ` data
37+ source is defined using [ ` driverFactory ` ] [ ref-config-ref-driverfactory] in your
38+ ` cube.js ` file.
39+
40+ </InfoBox >
41+
6042### Decorated environment variables
6143
6244Cube allows database-specific environment variables to be decorated with a data
@@ -74,16 +56,40 @@ CUBEJS_DS_DATASOURCE1_DB_TYPE=postgres
7456```
7557
7658For more information on environment variables that support decoration, check the
77- [ Environment Variables reference] [ ref-config-ref-env ] , or [ a database-specific
78- page if required] [ ref-config-db ] .
59+ [ environment variables reference] [ ref-config-ref-env ] or [ database-specific
60+ pages] [ ref-config-db ] .
61+
62+ ## Data model
7963
80- ## Dynamic data sources
64+ Use the [ ` dataSource ` ] ( /schema/reference/cube#parameters-data-source ) property
65+ to set a data source for each cube:
8166
82- For more complex scenarios involving multitenancy, Cube can also be configured
83- to dynamically lookup data sources. For more information, see [ the Multitenancy
84- page] [ ref-config-multitenancy ] .
67+ ``` javascript
68+ cube (' Orders' , {
69+ ... ,
70+
71+ dataSource: ' default' ,
72+ });
73+
74+ cube (' OrdersFromOtherDataSource' , {
75+ ... ,
76+
77+ dataSource: ' datasource1' ,
78+ });
79+ ```
8580
8681[ ref-config-ref-env ] : /reference/environment-variables
82+ [ ref-config-ref-driverfactory ] : /config#options-reference-driver-factory
8783[ ref-config-db ] : /config/databases
8884[ ref-config-multitenancy] :
8985 /config/multitenancy#multitenancy-multitenancy-vs-multiple-data-sources
86+
87+ ## Cube Cloud
88+
89+ Follow these steps to connect to multiple data sources in Cube Cloud:
90+
91+ - Set up the ` default ` database connection when creating a new deployment.
92+ - Ensure you have the correct [ multitenancy] ( /config/multitenancy ) configuration
93+ in your ` cube.js ` file.
94+ - Configure the corresponding environment variables in  ; <Btn >Settings →
95+ Environment variables</Btn >.
0 commit comments