Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deepmancer/metabase-postgres-docker
Automates the addition of PostgreSQL databases to a Metabase Docker container via the Metabase API. Simplifies data source integration.
https://github.com/deepmancer/metabase-postgres-docker
automation bash-script docker docker-compose metabase metabase-api metabase-data metabase-deployment postgres postgresql script
Last synced: about 1 month ago
JSON representation
Automates the addition of PostgreSQL databases to a Metabase Docker container via the Metabase API. Simplifies data source integration.
- Host: GitHub
- URL: https://github.com/deepmancer/metabase-postgres-docker
- Owner: deepmancer
- License: apache-2.0
- Created: 2024-08-05T07:53:53.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-08-16T12:03:45.000Z (3 months ago)
- Last Synced: 2024-09-27T01:23:19.183Z (about 2 months ago)
- Topics: automation, bash-script, docker, docker-compose, metabase, metabase-api, metabase-data, metabase-deployment, postgres, postgresql, script
- Language: Shell
- Homepage:
- Size: 59.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# π Metabase Automatic Docker Integration Script
Welcome to the **Metabase Automatic Docker Integration Script** repository! This handy Bash script automates the process of integrating PostgreSQL databases into your Metabase Docker container. By interacting with the Metabase API, this script streamlines database management, saving you time and reducing manual configuration.
---
## β¨ Features
This script does the heavy lifting for you:
1. **Automatic Network Configuration**: Fetches the gateway IP of your specified Docker network.
2. **Seamless Authentication**: Authenticates with the Metabase API to get a session token.
3. **Database Integration**: Adds your PostgreSQL databases to Metabase with just one command.## π¨ Prerequisites
Before you dive in, make sure you have these tools ready:
- **jq**: A lightweight and flexible command-line JSON processor.
- **Docker Compose**: To set up and manage your Metabase service with a PostgreSQL backend.Hereβs a sample `docker-compose.yml` to get you started:
```yaml
services:
metabase:
image: metabase/metabase:latest
container_name: metabase
hostname: metabase
ports:
- "3000:3000"
environment:
MB_DB_TYPE: "postgres"
MB_DB_DBNAME: "metabase_db"
MB_DB_PORT: "5432"
MB_DB_USER: "metabase_user"
MB_DB_PASS: "metabase_password"
MB_DB_HOST: "metabase_db"
MB_ADMIN_EMAIL: "[email protected]"
MB_USER_EMAIL: "[email protected]"
MB_USER_PASSWORD: "youpassword"
MB_DISABLE_SESSION_THROTTLE: "true"
MB_SOURCE_ADDRESS_HEADER: NULL
MB_SHOW_LIGHTHOUSE_ILLUSTRATION: "false"
MB_NO_SURVEYS: "true"
MB_LOAD_ANALYTICS_CONTENT: "false"
MB_ANON_TRACKING_ENABLED: "false"
MB_SETUP_TOKEN: ""
volumes:
- metabase_data:/metabase-data
depends_on:
- metabase_dbmetabase_db:
image: postgres:16.3
container_name: "metabase_db"
hostname: "metabase_db"
ports:
- "5858:5432"
environment:
POSTGRES_DB: "metabase_db"
POSTGRES_USER: "metabase_user"
POSTGRES_PASSWORD: "metabase_password"
volumes:
- metabase_db_data:/var/lib/postgresql/datavolumes:
metabase_data:
metabase_db_data:
```---
## π Script Breakdown
### 1. Configure Your Environment
Start by setting up your script with essential variables. Tailor these to match your Metabase setup and Docker network:
```bash
METABASE_URL="http://localhost:3000/api"
METABASE_USERNAME="[email protected]"
METABASE_PASSWORD="yourpassword"NETWORK_NAME="your_private_network_name"
```### 2. Find the Docker Network Gateway IP
The script will automatically retrieve the gateway IP for your Docker network. If it can't find one, it will default to `127.0.0.1`:
```bash
GATEWAY_IP=$(docker network inspect "$NETWORK_NAME" --format '{{range .IPAM.Config}}{{.Gateway}}{{end}}')
if [ -z "$GATEWAY_IP" ]; then
GATEWAY_IP="127.0.0.1"
echo "Gateway IP for network $NETWORK_NAME not found. Using default IP $GATEWAY_IP."
else
echo "Gateway IP for network $NETWORK_NAME is $GATEWAY_IP"
fi
```### 3. Define Your Database Configurations
Add the PostgreSQL databases you wish to integrate into the `DATABASES` array. The format is:
```
"NAME|NETWORK_IP|PORT|DB_NAME|USER|PASSWORD"
```For example:
```bash
DATABASES=(
"User Database|$GATEWAY_IP|5438|user_database|user_user|user_password"
# Add more databases here
)
```### 4. Authenticate with Metabase
This function logs into your Metabase instance and grabs a session token:
```bash
authenticate_metabase() {
echo "Authenticating with Metabase..."
SESSION_ID=$(curl -s -X POST -H "Content-Type: application/json" -d "{"username": "${METABASE_USERNAME}", "password": "${METABASE_PASSWORD}"}" "${METABASE_URL}/session" | jq -r '.id')if [ -z "$SESSION_ID" ]; then
echo "Authentication failed. Exiting."
exit 1
fiecho "Authenticated successfully. Session ID: ${SESSION_ID}"
}
```### 5. Add Your Databases to Metabase
This function sends a request to the Metabase API to add each database:
```bash
add_database() {
local NAME=$1
local HOST=$2
local PORT=$3
local DBNAME=$4
local USER=$5
local PASSWORD=$6echo "Adding database: $NAME..."
RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -H "X-Metabase-Session: ${SESSION_ID}" -d "{
"name": "${NAME}",
"engine": "postgres",
"details": {
"host": "${HOST}",
"port": ${PORT},
"dbname": "${DBNAME}",
"user": "${USER}",
"password": "${PASSWORD}"
}
}" "${METABASE_URL}/database")if echo "$RESPONSE" | jq -e '.id' >/dev/null; then
echo "Database '$NAME' added successfully."
else
echo "Failed to add database '$NAME'. Response: $RESPONSE"
fi
}
```### 6. Run the Script
Authenticate with Metabase, and then add your databases:
```bash
authenticate_metabasefor db in "${DATABASES[@]}"; do
IFS='|' read -r DB_NAME DB_HOST DB_PORT DB_DBNAME DB_USER DB_PASSWORD <<< "$db"
add_database "$DB_NAME" "$DB_HOST" "$DB_PORT" "$DB_DBNAME" "$DB_USER" "$DB_PASSWORD"
doneecho "Finished adding databases."
```## π How to Use
1. **Set Up a Metabase User**: Make sure you have a user account ready in your Metabase instance.
2. **Customize the Script**: Edit the configuration variables to match your Metabase setup and Docker network. Populate the `DATABASES` array with your PostgreSQL databases.
3. **Execute the Script**: Make the script executable and run it:
```bash
chmod +x add_datasources.sh
./add_datasources.sh
```4. **Verify the Results**: Log into your Metabase instance and check if your databases have been successfully added.
---
## π License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.