GitHub user dosubot[bot] added a comment to the discussion: Best method to 
automatically create users in Superset at startup?

<!-- Answer -->
Here are examples for each method of automatic user creation in Superset:

**1. Deployment Script / CLI Command Example**

You can create a demo user by running this command in your deployment script or 
manually:

```bash
superset fab create-user \
  --username demo \
  --firstname Demo \
  --lastname User \
  --email [email protected] \
  --password demopassword \
  --role Gamma
```
This works anywhere Superset is installed and is the recommended way for 
automation in scripts or CI/CD pipelines. 
[Reference](https://github.com/apache/superset/issues/34467)

---

**2. Docker Entrypoint Script Example**

In Docker setups, user creation is typically handled in the entrypoint/init 
script. Here’s a snippet based on Superset’s official `docker-init.sh`:

```bash
# Example: Add to your docker-init.sh or similar entrypoint script
superset db upgrade
superset fab create-user \
  --username demo \
  --firstname Demo \
  --lastname User \
  --email [email protected] \
  --password demopassword \
  --role Gamma
superset init
```
You can see the full context in the [official 
docker-init.sh](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/docker/docker-init.sh).

---

**3. Direct CLI Command Example**

You can run the same CLI command directly in your shell:

```bash
superset fab create-user \
  --username demo \
  --firstname Demo \
  --lastname User \
  --email [email protected] \
  --password demopassword \
  --role Gamma
```
This is identical to the deployment script method.

---

**4. Security API Automation (Superset 5.x+)**

First, enable the API in `superset_config.py`:

```python
FAB_ADD_SECURITY_API = True
```
Then run:
```bash
superset init
```
and restart Superset. 
[Reference](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/docs/docs/security/security.mdx)

**Automation Script Example (using `curl`):**

1. **Get JWT Token:**
   ```bash
   TOKEN=$(curl -s -X POST "http://localhost:8088/api/v1/security/login"; \
     -H "Content-Type: application/json" \
     -d '{"username":"admin","password":"admin","provider":"db"}' | jq -r 
.access_token)
   ```

2. **Get Role ID for 'Gamma':**
   ```bash
   ROLE_ID=$(curl -s -X GET "http://localhost:8088/api/v1/security/roles/"; \
     -H "Authorization: Bearer $TOKEN" | jq '.result[] | select(.name=="Gamma") 
| .id')
   ```

3. **Create Demo User:**
   ```bash
   curl -X POST "http://localhost:8088/api/v1/security/users/"; \
     -H "Authorization: Bearer $TOKEN" \
     -H "Content-Type: application/json" \
     -d '{
       "username": "demo",
       "first_name": "Demo",
       "last_name": "User",
       "email": "[email protected]",
       "password": "demopassword",
       "roles": ['$ROLE_ID']
     }'
   ```
The API expects role IDs, not names, so you must look up the role first. Only 
admin users can create new users via the API. [OpenAPI 
reference](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/docs/static/resources/openapi.json)

Let me know if you need a Python version or more details!


<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*

---
<sup>How did I do? 
[Good](https://app.dosu.dev/response-feedback/2c34a722-d1e0-4a4e-bae8-8f0f87b74beb?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/2c34a722-d1e0-4a4e-bae8-8f0f87b74beb?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/2c34a722-d1e0-4a4e-bae8-8f0f87b74beb?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/2c34a722-d1e0-4a4e-bae8-8f0f87b74beb?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/2c34a722-d1e0-4a4e-bae8-8f0f87b74beb?feedback_type=hallucination)
 | [Report 
🐛](https://app.dosu.dev/response-feedback/2c34a722-d1e0-4a4e-bae8-8f0f87b74beb?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/2c34a722-d1e0-4a4e-bae8-8f0f87b74beb?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat
 with 
Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github)&
 nbsp;[![Join 
Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share
 on 
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/discussions/35417)

GitHub link: 
https://github.com/apache/superset/discussions/35417#discussioncomment-14562138

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to