Cypher Shell
About Cypher Shell CLI
Cypher Shell is a command-line tool that comes with the Neo4j distribution. It can also be downloaded from Neo4j Download Center and installed separately.
Cypher Shell CLI is used to run queries and perform administrative tasks against a Neo4j instance. By default, the shell is interactive, but you can also use it for scripting, by passing cypher directly on the command line or by piping a file with cypher statements (requires PowerShell on Windows). It communicates via the Bolt protocol.
Syntax
The Cypher Shell CLI is located in the bin
directory if installed as part of the product.
Syntax:
cypher-shell [-h, --help] [-a ADDRESS, --address ADDRESS, --uri ADDRESS] [-u USERNAME, --username USERNAME] [-p PASSWORD, --password PASSWORD] [--encryption {true,false,default}] [-d DATABASE, --database DATABASE] [--impersonate IMPERSONATE] [--format {auto,verbose,plain}] [-P PARAM, --param PARAM] [--debug] [--non-interactive] [--sample-rows SAMPLE-ROWS] [--wrap {true,false}] [-v, --version] [--driver-version] [-f FILE, --file FILE] [--change-password] [--fail-fast] [--fail-at-end] [--log [LOG-FILE]] [cypher]
Arguments:
Argument | Type | Description | Default value |
---|---|---|---|
|
Optional argument |
Show help message and exit. |
|
|
Connection argument |
Address and port to connect to.
It can also be specified by the environment variable |
|
|
Connection argument |
Username to connect as.
It can also be specified by the environment variable |
|
|
Connection argument |
Password to connect with.
It can also be specified by the environment variable |
|
|
Connection argument |
Whether the connection to Neo4j should be encrypted; must be consistent with Neo4j’s configuration. |
The encryption setting is deduced from the specified address.
For example, the |
|
Connection argument |
Database to connect to.
It can also be specified by the environment variable |
|
|
Optional argument |
Desired output format. |
|
|
Connection argument |
Impersonate the specified user. |
No impersonation |
|
Optional argument |
Add a parameter to this session.
For example, |
|
|
Optional argument |
Print additional debug information. |
|
|
Optional argument |
Force non-interactive mode; only useful if auto-detection fails (e.g. Windows). |
|
|
Optional argument |
Number of rows sampled to compute table widths (only for format= |
|
|
Optional argument |
Wrap table column values if column is too narrow (only for format= |
|
|
Optional argument |
Print version of cypher-shell and exit. |
|
|
Optional argument |
Print version of the Neo4j Driver used and exit. |
|
|
Optional argument |
Pass a file with cypher statements to be executed. After the statements have been executed cypher-shell shuts down. |
|
|
Optional argument |
Change Neo4j user password and exit |
|
|
Optional argument |
Exit and report failure on first error when reading from file. |
This is the default behavior. |
|
Optional argument |
Exit and report failures at end of input when reading from file. |
|
|
Optional argument |
Enables logging to the specified file, or |
|
|
Positional argument |
An optional string of cypher to execute and then exit. |
Running Cypher Shell within the Neo4j distribution
You can connect to a live Neo4j DBMS by running cypher-shell
and passing in a username and a password argument:
bin/cypher-shell -u neo4j -p <password>
The output is the following:
Connected to Neo4j at neo4j://localhost:7687 as user neo4j. Type :help for a list of available commands or :exit to exit the shell. Note that Cypher queries must end with a semicolon.
Running Cypher Shell from a different server
You can also install the Cypher Shell tool on a different server (without Neo4j) and connect to a Neo4j DBMS. Cypher Shell requires a JDK and Java 11.
DEB/RPM distributions both install OpenJDK if it is not already installed. The cypher-shell files are available in the same DEB/RPM Linux repositories as Neo4j. The TAR distribution contains only the cypher-shell files, so you must install the JDK manually. |
-
Download Cypher Shell from Neo4j Download Center.
-
Connect to a Neo4j DBMS by running the
cypher-shell
command providing the Neo4j address, a username, and a password:cypher-shell/cypher-shell -a neo4j://IP-address:7687 -u neo4j -p <password>
The output is the following:
Connected to Neo4j at neo4j://IP-address:7687 as user neo4j. Type :help for a list of available commands or :exit to exit the shell. Note that Cypher queries must end with a semicolon.
Available commands
Once in the interactive shell, run the following command to display all available commands:
help
:help
The output is the following:
Available commands: *:begin* Open a transaction *:commit* Commit the currently open transaction *:connect* Connects to a database *:disconnect* Disconnects from database *:exit* Exit the logger *:help* Show this help message *:history* Statement history *:impersonate* Impersonate user *:param* Set the value of a query parameter *:params* Print all query parameter values *:rollback* Rollback the currently open transaction *:source* Executes Cypher statements from a file *:use* Set the active database For help on a specific command type: :help *command* Keyboard shortcuts: Up and down arrows to access statement history. Tab for autocompletion of commands, hit twice to select suggestion from list using arrow keys.
Running Cypher statements
You can run Cypher statements in the following ways:
-
Typing Cypher statements directly into the interactive shell.
-
Running Cypher statements from a file with the interactive shell.
-
Running Cypher statements from a file as a
cypher-shell
argument.
The examples in this section use the MATCH (n) RETURN n LIMIT 5
Cypher statement and will return 5 nodes from the database.
MATCH (n) RETURN n LIMIT 5;
The following two examples assume a file exists in the same folder you run the
|
You can use the :source
command followed by the file name to run the Cypher statements in that file when in the Cypher interactive shell:
:source example.cypher
cypher-shell
argument.You can pass a file containing Cypher statements as an argument when running cypher-shell
.
The examples here use the --format plain
flag for a simple output.
Using cat
(UNIX)
cat example.cypher | bin/cypher-shell -u neo4j -p <password> --format plain
Using type
(Windows)
type example.cypher | bin/cypher-shell.bat -u neo4j -p <password> --format plain
Query parameters
Cypher Shell CLI supports querying based on parameters. This is often used while scripting.
-
Set the parameter
thisAlias
toRobin
using the:param
keyword::param thisAlias => 'Robin'
-
Check the parameter using the
:params
keyword::params
:param thisAlias => 'Robin'
-
Now use the parameter
thisAlias
in a Cypher query:CREATE (:Person {name : 'Dick Grayson', alias : $thisAlias });
Added 1 nodes, Set 2 properties, Added 1 labels
-
Verify the result:
MATCH (n) RETURN n;
+-----------------------------------------------------------------+ | n | +-----------------------------------------------------------------+ | (:Person {name: "Bruce Wayne", alias: "Batman"}) | | (:Person {name: "Selina Kyle", alias: ["Catwoman", "The Cat"]}) | | (:Person {name: "Dick Grayson", alias: "Robin"}) | +-----------------------------------------------------------------+ 3 rows available after 2 ms, consumed after another 2 ms
Transactions
Cypher Shell supports explicit and implicit transactions.
Transaction states are controlled using the keywords :begin
, :commit
, and :rollback
.
Both explicit and implicit transactions run from Cypher Shell will have default transaction metadata attached that follows the convention (see Attach metadata to a transaction).
The example uses the dataset from the built-in Neo4j Browser guide, called MovieGraph. For more information, see the Neo4j Browser documentation.
-
Run a query that shows there is only one person in the database, who is born in 1964.
MATCH (n:Person) WHERE n.born=1964 RETURN n.name AS name;
+----------------+ | name | +----------------+ | "Keanu Reeves" | +----------------+ 1 row ready to start consuming query after 9 ms, results consumed after another 0 ms
-
Start a transaction and create another person born in the same year:
:begin neo4j# CREATE (:Person {name : 'Edward Mygma', born:1964});
0 rows ready to start consuming query after 38 ms, results consumed after another 0 ms Added 1 nodes, Set 2 properties, Added 1 labels
-
If you open a second Cypher Shell session and run the query from step 1, you will notice no changes from the latest
CREATE
statement.MATCH (n:Person) WHERE n.born=1964 RETURN n.name AS name;
+----------------+ | name | +----------------+ | "Keanu Reeves" | +----------------+ 1 row ready to start consuming query after 9 ms, results consumed after another 0 ms
-
Go back to the first session and commit the transaction.
neo4j# :commit
-
Now, if you run the query from step 1, you will see that Edward Mygma has been added to the database.
MATCH (n:Person) WHERE n.born=1964 RETURN n.name AS name;
+----------------+ | name | +----------------+ | "Keanu Reeves" | | "Edward Mygma" | +----------------+ 2 rows ready to start consuming query after 1 ms, results consumed after another 1 ms
Procedures
Cypher Shell supports running any procedures for which the current user is authorized.
dbms.showCurrentUser
procedureCALL dbms.showCurrentUser();
+------------------------------+ | username | roles | flags | +------------------------------+ | "neo4j" | ["admin"] | [] | +------------------------------+ 1 row available after 66 ms, consumed after another 2 ms
Supported operating systems
You can use the Cypher Shell CLI via cmd
on Windows systems, and bash
on Unix systems.
Other shells may work as intended, but there is no test coverage to guarantee compatibility.
Keyboard shortcuts
The following keyboard commands are available in interactive mode.
Key | Operation |
---|---|
↑ and ↓ (arrow keys) |
Access statement history. |
↹ (tab) |
Autocompletion of commands and Cypher syntax. Suggestions for Cypher syntax is not complete. |
Was this page helpful?