You connect to a database by creating a Driver object and providing a URL and an authentication token.
from neo4j import GraphDatabase # URI examples: "neo4j://localhost", "neo4j+s://xxx.databases.neo4j.io" URI = "<URI for Neo4j database>" AUTH = ("<Username>", "<Password>") with GraphDatabase.driver(URI, auth=AUTH) as driver: (1) driver.verify_connectivity() (2)
|2||To verify immediately that the driver can connect to the database (valid credentials, compatible versions, etc), use the
Both the creation of a
Driver object and the connection verification can raise a number of different exceptions.
Since error handling can get quite verbose, and a connection error is a blocker for any subsequent task, the most common choice is to let the program crash should an exception occur during connection.
Driver objects are immutable, thread-safe, and fairly expensive to create.
Share them across threads (but not across processes) and use impersonation to query the database with a different user.
If you want to alter a
Driver configuration, you will need to create a new object.
When you create an Aura instance, you may download a text file containing the connection information to the database.
The file has a name of the form
To connect to such an instance, you may either use the URI, username, and password explicitly in your application, or load the content of the connection file in the environment with
dotenv.load_dotenv() and populate your local variables via
This approach requires the package
import dotenv import os from neo4j import GraphDatabase dotenv.load_dotenv("Neo4j-a0a2fa1d-Created-2023-11-06.txt") URI = os.getenv("NEO4J_URI") AUTH = (os.getenv("NEO4J_USERNAME"), os.getenv("NEO4J_PASSWORD")) with GraphDatabase.driver(URI, auth=AUTH) as driver: driver.verify_connectivity()
Driver objects to free up all allocated resources, even upon unsuccessful connection or runtime errors.
Either instantiate the
Driver object using the
with statement, or call the
Driver.close() method explicitly.
Driver configuration parameters and further connection settings, see Advanced connection information.
A Long Term Support release is one guaranteed to be supported for a number of years. Neo4j 4.4 is LTS, and Neo4j 5 will also have an LTS version.
Aura is Neo4j’s fully managed cloud service. It comes with both free and paid plans.
Cypher is Neo4j’s graph query language that lets you retrieve data from the database. It is like SQL, but for graphs.
Awesome Procedures On Cypher (APOC) is a library of (many) functions that can not be easily expressed in Cypher itself.
Bolt is the protocol used for interaction between Neo4j instances and drivers. It listens on port 7687 by default.
Atomicity, Consistency, Isolation, Durability (ACID) are properties guaranteeing that database transactions are processed reliably. An ACID-compliant DBMS ensures that the data in the database remains accurate and consistent despite failures.
- eventual consistency
A database is eventually consistent if it provides the guarantee that all cluster members will, at some point in time, store the latest version of the data.
- causal consistency
A database is causally consistent if read and write queries are seen by every member of the cluster in the same order. This is stronger than eventual consistency.
The null marker is not a type but a placeholder for absence of value. For more information, see Cypher Manual — Working with
A transaction is a unit of work that is either committed in its entirety or rolled back on failure. An example is a bank transfer: it involves multiple steps, but they must all succeed or be reverted, to avoid money being subtracted from one account but not added to the other.
Backpressure is a force opposing the flow of data. It ensures that the client is not being overwhelmed by data faster than it can handle.
- transaction function
A transaction function is a callback executed by an
execute_writecall. The driver automatically re-executes the callback in case of server failure.
Driverobject holds the details required to establish connections with a Neo4j database.