How to Add a Database to Metabase via CLI Using the REST API
Introduction
Metabase is a popular open-source business intelligence platform that allows users to connect various databases and create dashboards and reports. While databases are typically added through the Metabase web interface, administrators can automate the process using the Metabase REST API and command-line tools such as curl.
This article explains how to add a database to Metabase from the command line.
Prerequisites
Before proceeding, ensure the following:
- Metabase is installed and running.
- Administrator access to Metabase.
- API key or username and password for authentication.
- Database connection details (host, port, database name, username, and password).
curlinstalled on the server.
Implementation
Step 1: Generate an API Key
Navigate to:
Admin Settings → Authentication → API Keys
Generate an API key and note it for use in API requests.
Step 2: Create the Database Connection
Execute the following command to add a MySQL database:
curl -X POST http://metabase.example.com/api/database \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_API_KEY" \
-d '{
"engine": "mysql",
"name": "Production MySQL",
"details": {
"host": "IP ADDRESS",
"port": 3306,
"dbname": "employees",
"user": "dbuser",
"password": "dbpassword"
}
}'
Step 3: Verify the Database
Retrieve the list of databases configured in Metabase:
curl -X GET http://metabase.example.com/api/database \
-H "x-api-key: YOUR_API_KEY"
The output will display all configured databases.
Step 4: Update an Existing Database
To modify an existing database configuration:
curl -X PUT http://metabase.example.com/api/database/2 \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_API_KEY" \
-d '{
"name": "Updated MySQL Database"
}'
Replace 2 with the database ID.
Conclusion
Although Metabase does not provide a dedicated CLI command for adding databases, administrators can automate the process using the REST API and command-line tools such as curl. This approach is particularly useful for scripting, infrastructure automation, and managing multiple Metabase instances.
