# Creating database and users automatically

**URL:** https://forums.percona.com/t/creating-database-and-users-automatically/16584
**Category:** Percona Operator for MongoDB
**Created:** [July 15, 2022, 8:04am UTC](https://forums.percona.com/t/creating-database-and-users-automatically/16584 "2022-07-15T08:04:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![aydink](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@aydink](https://forums.percona.com/u/aydink)
#### Post date: [July 15, 2022, 8:04am UTC](https://forums.percona.com/t/creating-database-and-users-automatically/16584/1 "2022-07-15T08:04:57Z")

</div>

Hi!

am taking rocketchat example ([Running Rocket.Chat with Percona Server for MongoDB on Kubernetes - Percona Database Performance Blog](https://www.percona.com/blog/percona-mongodb-operator-kubernetes-rocket-chat)) as base for my current question.  
In the rocketchat example, databases and users are created by connecting to the mongo pod and executing mongo-statements manually:

```auto
use rocketchat
db.createUser({
  user: "rocketChat",
  pwd: passwordPrompt(),
  roles: [
    { role: "readWrite", db: "rocketchat" }
  ]
})

use admin
db.createUser({
  user: "oplogger",
  pwd: passwordPrompt(),
  roles: [
    { role: "read", db: "local" }
  ]
})

```

Is there any automatism possibility which can take care of this?  
Something like providing client-specific databases and users via YAML (feeding the operator with instructions like creating a database, create users etc) or at least from command line (e.g. having a one-liner for kubectl would also be sufficient. something like providing the mongo statements as arguments to the percona-mongo-client pod)?

Background is, that we are using CICD pipelines and need to automate things like prepopulating databases and users.

Great job btw.

---

<div class="post-metadata">

### Author: ![Ege\_Gunes](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/ege_gunes/32/4595_2.png) [@Ege\_Gunes](https://forums.percona.com/u/Ege_Gunes)
#### Post date: [July 15, 2022, 9:11am UTC](https://forums.percona.com/t/creating-database-and-users-automatically/16584/2 "2022-07-15T09:11:50Z")

</div>

Hi @aydink,

The operator doesn’t support user management, it just manages the infrastructure.

Here’s a oneliner to get your started:

```auto
kubectl exec ${client_container} -- \
		bash -c "printf 'db.createUser({user: "myApp", pwd: "myPass", roles: [{ db: "myApp", role: "readWrite" }]})\n' | mongo mongodb://${MONGODB_USER_ADMIN_USER}:${MONGODB_USER_ADMIN_PASSWORD}@${cluster}.${namespace}/admin?ssl=false&replicaSet=rs0"

```

---

<div class="post-metadata">

### Author: ![aydink](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@aydink](https://forums.percona.com/u/aydink)
#### Post date: [July 15, 2022, 10:21am UTC](https://forums.percona.com/t/creating-database-and-users-automatically/16584/3 "2022-07-15T10:21:11Z")

</div>

Thanks a lot!

Based on your suggestion, I shortened it to (without the need of an existing client container):

`kubectl run -i --rm --tty percona-client1 --image=percona/percona-server-mongodb:4.4.10-11 --restart=Never -- bash -c "printf 'db.createUser({user: \"admin\", pwd: \"secret\", roles: [{ db: \"messages\", role: \"readWrite\" }]})\n' | mongo mongodb://mongoadmin:secret@my-mongo-db/admin"`
