Search code examples
pythonpython-3.xneo4jpy2neo

How to check connection from Python to Neo4j


I'm doing a microservice in Python 3.7 that connects to a Neo4j database. It's the first time I work connecting Python with Neo4j and I'm using py2neo version 4.3.0. Everything works OK, but now to adhere to the standard, I need to create a healthcheck to verify the connection to the Database. I wanted to use the

from py2neo import Graph, Database

and use

db = Database ("bolt: // localhost: 7474", auth = ("neo4j", "xxxx"))

and

db.kernel_version (Dont work)

but with this I do not verify that there is connection is up. Does anybody have any suggestions?


Solution

  • If checking the kernel version doesn't work then the connection is not ok. Below is a script to check if the connection from python to neo4j (via py2neo) is up and running.

    from py2neo import Graph
    graph = Graph("bolt://localhost:7687", auth=("neo4j", "xxxxx"))
    try:
        graph.run("Match () Return 1 Limit 1")
        print('ok')
    except Exception:
        print('not ok')