# Admin commands to create multiple DBs in Python

**URL:** <https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908>\
**Category:** Python\
**Created:** [May 6, 2021, 11:16am UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908 "2021-05-06T11:16:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ariel.degraaf](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ariel.degraaf/32/4694_2.png) [@ariel.degraaf](https://community.neo4j.com/u/ariel.degraaf)\
**Post date:** [May 6, 2021, 11:16am UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908/1 "2021-05-06T11:16:23Z")

</div>

Hello,

Is it possible to create a new database with a specific name via the Python-Neo4j driver?  
For example, executing the following Admin commands in a Python script:

:USE system  
CREATE DATABASE dbname

Thanks, Ariel

---

<div class="post-metadata">

**Author:** ![ariel.degraaf](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ariel.degraaf/32/4694_2.png) [@ariel.degraaf](https://community.neo4j.com/u/ariel.degraaf)\
**Post date:** [May 6, 2021, 1:32pm UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908/3 "2021-05-06T13:32:15Z")

</div>

Too bad...,  
but thanks anyhow!

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [May 6, 2021, 1:53pm UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908/4 "2021-05-06T13:53:53Z")

</div>

You can maybe do a batch (.bat) script do achieve it. Then use Python to execute the batch script 🙂

---

<div class="post-metadata">

**Author:** ![ariel.degraaf](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ariel.degraaf/32/4694_2.png) [@ariel.degraaf](https://community.neo4j.com/u/ariel.degraaf)\
**Post date:** [May 6, 2021, 2:20pm UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908/5 "2021-05-06T14:20:54Z")

</div>

Thanks for the suggestion!  
I was thinking to create a TXT file with the names of all the databases that I want to create, e.g.:

:USE system  
CREATE DATABASE db1;  
CREATE DATABASE db2;

...

and to paste this manually into the Neo4j Browser. I would need to do this only once.

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [May 6, 2021, 2:23pm UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908/6 "2021-05-06T14:23:15Z")

</div>

Yeah you could do this, it's the more easier 🙂

---

<div class="post-metadata">

**Author:** ![rouven\_bauer](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rouven_bauer/32/16547_2.png) [@rouven\_bauer](https://community.neo4j.com/u/rouven_bauer)\
**Post date:** [May 6, 2021, 2:37pm UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908/7 "2021-05-06T14:37:36Z")

</div>

It is possible to create a database through the driver. Here is a code snippet:

```python
from neo4j import GraphDatabase

driver = GraphDatabase.driver(uri, auth=(username, password))

with driver.session(database="system") as session:
    with session.begin_transaction() as tx:
        res = tx.run("CREATE DATABASE db1 IF NOT EXISTS")
    bookmark = session.last_bookmark()

with driver.session(database="db1", bookmarks=(bookmark,)) as session:
    with session.begin_transaction() as tx:
        res = tx.run('CREATE (n:Node {foo: "bar"}) RETURN n')
        print(list(res))

```

Note that I'm passing along the bookmark of the session that creates the database to the session that uses the database. If you don't do this, you might end up with an error because the second session tries to start the transaction on `db1` before it has been fully created.

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [May 6, 2021, 2:44pm UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908/8 "2021-05-06T14:44:34Z")

</div>

I didn't know you could do this 😮

---

<div class="post-metadata">

**Author:** ![ariel.degraaf](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ariel.degraaf/32/4694_2.png) [@ariel.degraaf](https://community.neo4j.com/u/ariel.degraaf)\
**Post date:** [May 6, 2021, 3:45pm UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908/9 "2021-05-06T15:45:38Z")

</div>

Thank you so much Rouven!  
Although I already chose for the "manual" solution, it is good to know that the Python driver has this great option.

---

<div class="post-metadata">

**Author:** ![mohamed\_elzonko](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mohamed_elzonko/32/22865_2.png) [@mohamed\_elzonko](https://community.neo4j.com/u/mohamed_elzonko)\
**Post date:** [April 29, 2022, 11:16pm UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908/10 "2022-04-29T23:16:10Z")

</div>

This is really useful.. Didn't know this. Thank you!

---

<div class="post-metadata">

**Author:** ![rouven\_bauer](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rouven_bauer/32/16547_2.png) [@rouven\_bauer](https://community.neo4j.com/u/rouven_bauer)\
**Post date:** [October 11, 2023, 9:46am UTC](https://community.neo4j.com/t/admin-commands-to-create-multiple-dbs-in-python/37908/11 "2023-10-11T09:46:58Z")

</div>

If anyone stumbles over this in the future, I'd like to add that bookmarks alone might not be enough depending on the server version you're running against.

You might need a `WAIT` clause like this

```python
tx.run("CREATE DATABASE db1 IF NOT EXISTS WAIT")

```

See also: [https://www.neo4j.com/docs/cypher-manual/current/administration/databases/#administration-wait-nowait](https://www.neo4j.com/docs/cypher-manual/current/administration/databases/#administration-wait-nowait)
