1

I have connected to multi-database model in neo4j, Also have a set of python scripts to push and pull data to and from multiple database in NEO4J. We need to specify the name of the database we are operating these scripts against. Also to this i want to find currently which database which driver connected.

from neo4j import GraphDatabase
# create the uri for the connection
uri = "neo4j://localhost:7687"

# create a driver object
driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))
print("driver created")

# get info about the driver
print("connected: {}".format(driver.verify_connectivity()))


print("Host: {}".format(driver.default_host))
print("Port: {}".format(driver.default_port))

# close the driver
driver.close()

Here

print("connected: {}".format(driver.verify_connectivity())) 

this given me the info about the driver below only:

connected: Neo4j/4.4.11

Along this, as part of integration, i want to list the database with which driver connected. How can i achieve that, appreciate your help ?

Tono Kuriakose
  • 718
  • 6
  • 19

2 Answers2

1

If by

I want to list the database with which driver connected

you mean the server, you can use driver.get_server_info() https://neo4j.com/docs/api/python-driver/5.7/api.html#neo4j.Driver.get_server_info

If you mean against which database in your DBMS a query would be executed: that's whatever the currently configured home database of the user you're authenticated as (neo4j in your example) is.

If you look at https://neo4j.com/docs/operations-manual/current/manage-databases/configuration/ you can see that SHOW HOME DATABASE will give you the current home database.

Here is a quick example to show how that could look like:

import neo4j


URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")


with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
    print(driver.execute_query("SHOW HOME DATABASE").records[0]["name"])

Or little more elaborate and production ready:

with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
    print(
        driver.execute_query(
            "SHOW HOME DATABASE",
            database_="system",  # (1)
            routing_="r",  # (2)
            result_transformer_=lambda r: r.single(strict=True)  # (3)
        )["name"]
    )
  • (1) specifying the database makes the driver work slightly more efficient as it doesn't have to resolve the home database before executing the actual query (always recommend, when possible)
  • (2) tell the driver that this is a read query and in a clustered environment can be routed to followers/reader to reduce load on the leader/writer server
  • (3) turn the result stream into a single record and (strict=True) raise an exception if there's not exactly one record

This assumes you're using a relatively recent version of the driver. If not, you can achieve the same thing using lower-level APIs like driver.session and session.execute_read.

Robsdedude
  • 1,292
  • 16
  • 25
1
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 i in result:
        print(i['name'])

This will gives the Databases names

Felix
  • 46
  • 3