how to get all database names in neo4j using python?

Viewed 72
from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

with driver.session() as session:
        result = session.run("SHOW DATABASES")
        print(result)

the result is <neo4j.work.result.Result object at 0x0000028F2BB30208>

How to get database names?

2 Answers

Add this code, this works for me

 databasesNames = list()
 queryDatabases="SHOW DATABASES;" 
 try:
     driver=GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))
     session=driver.session(database='system')
     result = session.run(queryDatabases)
     for record in result:
         databasesNames.append(record["name"])
     exit
  except Exception as e:
        print("Connection to neo4j failed: ", e)
  print(databasesNames)

This will show you only the DATABASES names in cypher-shell:

  SHOW DATABASES yield name;

Example from python:

test.py

from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

with driver.session() as session:
    result = session.run("SHOW DATABASES yield name;")
    for line in result:
        print(line['name'])

When executed:

 python test.py 
 neo4j
 system
  

Tested in neo4j 4.2.1 , openjdk 11.0.15 2022-04-19 , python 3.7.0 , Ubuntu 20.04.4

Related