Managing databases
Neo4j supports the management of multiple databases within the same DBMS.
The metadata for these databases, including the associated security model, is maintained in a special database called the system
database.
All multi-database administrative commands must be run against the system
database.
These administrative commands are automatically routed to the system
database when connected to the DBMS over Bolt.
Administrative commands should not be used during a rolling upgrade. For more information, see Upgrade and Migration Guide → Upgrade a cluster. |
Listing databases
There are four different commands for listing databases, depending on whether you want to show:
-
All databases.
-
A particular database.
-
The default database.
-
The home database.
These commands return the following columns:
Column | Description | Type |
---|---|---|
|
The name of the database. Default Output |
|
|
The type of the database: |
|
|
The names of any aliases the database may have. Default Output |
|
|
The database access mode, either |
|
|
The database unique ID. |
|
|
The server instance ID. |
|
|
Instance address in a clustered DBMS.
The default for a standalone database is |
|
|
The current role of the database ( |
|
|
|
|
|
The expected status of the database.
The value can be either |
|
|
The actual status of the database. Default Output The possible statuses are:
See Database states for more information. |
|
|
A message explaining the status of the database, often explaining why it is not in the correct state. Default Output |
|
|
Show if this is the default database for the DBMS. Default Output
Not returned by |
|
|
Shown if this is the home database for the current user. Default Output
Not returned by |
|
|
Number of primaries for this database reported as running currently.
It is the same as the number of rows where |
|
|
Number of secondaries for this database reported as running currently.
It is the same as the number of rows where |
|
|
The requested number of primaries for this database. May be lower than current if the DBMS is currently reducing the number of copies of the database, or higher if it is currently increasing the number of copies. |
|
|
The requested number of secondaries for this database. May be lower than current if the DBMS is currently reducing the number of copies of the database, or higher if it is currently increasing the number of copies. |
|
|
The date and time at which the database was created. |
|
|
The date and time at which the database was last started. |
|
|
The date and time at which the database was last stopped. |
|
|
Information about the storage engine and the store format. The value is a string formatted as:
|
|
|
The ID of the last transaction received. |
|
|
Number of transactions the current database is behind compared to the database on the primary instance.
The lag is expressed in negative integers. In standalone environments, the value is always |
|
|
The names of any constituents the database may have. Applicable only for composite databases. Default Output |
|
|
The map of options applied to the database. |
|
The results of the SHOW DATABASES
command are filtered according to the ACCESS
privileges of the user.
However, some privileges enable users to see additional databases regardless of their ACCESS
privileges:
-
Users with
CREATE/DROP/ALTER DATABASE
orSET DATABASE ACCESS
privileges can see all standard databases. -
Users with
CREATE/DROP COMPOSITE DATABASE
orCOMPOSITE DATABASE MANAGEMENT
privileges can see all composite databases. -
Users with
DATABASE MANAGEMENT
privilege can see all databases.
If a user has not been granted ACCESS
privilege to any databases nor any of the above special cases, the command can still be executed but it will only return the system
database, which is always visible.
Database states
A database’s currentStatus
can be one of the following:
State | Description |
---|---|
|
The database is running. |
|
The database is not running.
If the |
|
The database is not running, but is about to. |
|
The database is not running anymore, but still has not stopped completely.
No offline operations (e.g. |
|
The database is currently being updated from another instance of Neo4j. |
|
The database has not yet been created. |
|
Only applies to databases in a cluster.
The database is still online but will eventually be offline due to a transfer of its role in the cluster to a different member.
The status is |
|
This state implies an error has occurred.
The database’s underlying store files may be invalid.
For more information, consult the |
|
The database is effectively stopped and its state may not be changed until no longer quarantined.
For more information, consult the |
|
This instance of Neo4j does not know the state of this database. |
Note that for failed databases, the |
Show all available databases
A summary of all available databases can be displayed using the command SHOW DATABASES
.
SHOW DATABASES
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | name | type | aliases | access | address | role | writer | requestedStatus | currentStatus | statusMessage | default | home | constituents | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | "movies" | "standard" | ["films", "motion pictures"] | "read-write" | "localhost:7687" | "primary" | TRUE | "online" | "online" | "" | FALSE | FALSE | [] | | "neo4j" | "standard" | [] | "read-write" | "localhost:7687" | "primary" | TRUE | "online" | "online" | "" | TRUE | TRUE | [] | | "system" | "system" | [] | "read-write" | "localhost:7687" | "primary" | TRUE | "online" | "online" | "" | FALSE | FALSE | [] | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
As of Neo4j 5.3, databases hosted on servers that are offline are also returned by the |
Show detailed information for a particular database
In this example, the detailed information for a particular database can be displayed using the command SHOW DATABASE name YIELD *
.
When a YIELD
clause is provided, the full set of columns is returned.
SHOW DATABASE movies YIELD *
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | name | type | aliases | access | databaseID | serverID | address | role | writer | requestedStatus | currentStatus | statusMessage | default | home | currentPrimariesCount | currentSecondariesCount | requestedPrimariesCount | requestedSecondariesCount | creationTime | lastStartTime | lastStopTime | store | lastCommittedTxn | replicationLag | constituents | options | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | "movies" | "standard" | ["films", "motion pictures"] | "read-write" | "C066801F54B44EA1520F0FE392B4005AABF42D8DD0A5FD09969B955575D287D5" | "e3063985-e2f4-4728-824b-a7d53779667a" | "localhost:7687" | "primary" | TRUE | "online" | "online" | "" | FALSE | FALSE | 1 | 0 | 1 | 0 | 2023-08-14T10:01:29.074Z | 2023-08-14T10:01:29.074Z | NULL | "record-aligned-1.1" | 3 | 0 | [] | {} | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Show the number of databases
The number of distinct databases can be seen using YIELD
and a count()
function in the RETURN
clause.
SHOW DATABASES YIELD name
RETURN count(DISTINCT name) AS count
+-------+ | count | +-------+ | 3 | +-------+
By specifying the name
column and sorting the results by distinct name, only the number of distinct databases are counted, not the number of allocations of databases in a clustered environment.
Show the default database
The default database can be seen using the command SHOW DEFAULT DATABASE
.
SHOW DEFAULT DATABASE
+--------------------------------------------------------------------------------------------------------------------------------------------------------+ | name | type | aliases | access | address | role | writer | requestedStatus | currentStatus | statusMessage | constituents | +--------------------------------------------------------------------------------------------------------------------------------------------------------+ | "neo4j" | "standard" | [] | "read-write" | "localhost:7687" | "primary" | TRUE | "online" | "online" | "" | [] | +--------------------------------------------------------------------------------------------------------------------------------------------------------+
Show the home database
The home database for the current user can be seen using the command SHOW HOME DATABASE
.
SHOW HOME DATABASE
+--------------------------------------------------------------------------------------------------------------------------------------------------------+ | name | type | aliases | access | address | role | writer | requestedStatus | currentStatus | statusMessage | constituents | +--------------------------------------------------------------------------------------------------------------------------------------------------------+ | "neo4j" | "standard" | [] | "read-write" | "localhost:7687" | "primary" | TRUE | "online" | "online" | "" | [] | +--------------------------------------------------------------------------------------------------------------------------------------------------------+
Filter the listed databases
It is also possible to filter and sort the results by using YIELD
, ORDER BY
, and WHERE
.
SHOW DATABASES YIELD name, currentStatus, requestedStatus
ORDER BY currentStatus
WHERE name CONTAINS 'o'
In this example:
-
The number of columns returned has been reduced with the
YIELD
clause. -
The order of the returned columns has been changed.
-
The results are ordered by the
currentStatus
column usingORDER BY
. -
The results have been filtered to only show database names containing
'o'
.
It is also possible to use SKIP
and LIMIT
to paginate the results.
+--------------------------------------------+ | name | currentStatus | requestedStatus | +--------------------------------------------+ | "movies" | "online" | "online" | | "neo4j" | "online" | "online" | +--------------------------------------------+
Create databases
Databases can be created using CREATE DATABASE
.
Database names are subject to the rules specified in Database names section.
Having dots ( |
CREATE DATABASE customers
When a database has been created, it will show up in the listing provided by the command SHOW DATABASES
.
SHOW DATABASES YIELD name
+-------------+ | name | +-------------+ | "customers" | | "movies" | | "neo4j" | | "system" | +-------------+
Use IF EXISTS
or OR REPLACE
when creating databases
The CREATE DATABASE
command is optionally idempotent, with the default behavior to fail with an error if the database already exists.
There are two ways to circumvent this behavior.
First, appending IF NOT EXISTS
to the command ensures that no error is returned and nothing happens should the database already exist.
CREATE DATABASE customers IF NOT EXISTS
Second, adding OR REPLACE
to the command results in any existing database being deleted and a new one being created.
CREATE OR REPLACE DATABASE customers
This is equivalent to running DROP DATABASE customers IF EXISTS
followed by CREATE DATABASE customers
.
The behavior of IF EXISTS
and OR REPLACE
apply to both standard and composite databases (e.g. a composite database may replace a standard database or another composite database).
The |
Create database options
The CREATE DATABASE
command can have a map of options, e.g. OPTIONS {key: 'value'}
.
Key | Value | Description |
---|---|---|
|
|
Controls how the system handles existing data on disk when creating the database.
Currently this is only supported with |
|
ID of the cluster server |
Defines which server is used for seeding the data of the created database.
The server ID can be found in the |
|
URI to a backup or a dump from an existing database. |
Defines an identical seed from an external source which will be used to seed all servers. |
|
comma separated list of configuration values. |
Defines additional configuration specified by comma separated |
|
credentials |
Defines credentials that need to be passed into certain seed providers. |
|
|
Defines level of enrichment applied to transaction logs for Change Data Capture (CDC) purposes. For details about enrichment mode, see Change Data Capture Manual → Getting Started → Enrichment mode. |
The |
Alter databases
Standard databases can be modified using the command ALTER DATABASE
.
Alter database access mode
By default, a database has read-write access mode on creation.
The database can be limited to read-only mode on creation using the configuration parameters dbms.databases.default_to_read_only
, dbms.databases.read_only
, and dbms.database.writable
.
For details, see the section on Configuration parameters.
A database that was created with read-write access mode can be changed to read-only.
To change it to read-only, you can use the ALTER DATABASE
command with the sub-clause SET ACCESS READ ONLY
.
Subsequently, the database access mode can be switched back to read-write using the sub-clause SET ACCESS READ WRITE
.
Altering the database access mode is allowed at all times, whether a database is online or offline.
If conflicting modes are set by the ALTER DATABASE
command and the configuration parameters, i.e. one says read-write and the other read-only, the database will be read-only and prevent write queries.
Modifying access mode is only available to standard databases and not composite databases. |
Alter database access mode to read-only
ALTER DATABASE customers SET ACCESS READ ONLY
The database access mode can be seen in the access
output column of the command SHOW DATABASES
.
SHOW DATABASES yield name, access
+----------------------------+ | name | access | +----------------------------+ | "customers" | "read-only" | | "movies" | "read-write" | | "neo4j" | "read-write" | | "system" | "read-write" | +----------------------------+
Alter database access using IF EXISTS
ALTER DATABASE
commands are optionally idempotent, with the default behavior to fail with an error if the database does not exist.
Appending IF EXISTS
to the command ensures that no error is returned and nothing happens should the database not exist.
ALTER DATABASE nonExisting IF EXISTS
SET ACCESS READ WRITE
Alter database topology
In a cluster environment, you can use the ALTER DATABASE
command to change the number of servers hosting a database.
For more information, see Managing databases in a cluster.
Alter database options
The ALTER DATABASE
command can be used to set or remove specific options for a database.
Key | Value | Description |
---|---|---|
|
|
Defines the level of enrichment applied to transaction logs for Change Data Capture (CDC) purposes. For details about enrichment mode, see Change Data Capture Manual → Getting Started → Enrichment mode. |
There are no available |
Alter the options set for a database
ALTER DATABASE `movies`
SET OPTION txLogEnrichment 'FULL'
The database set options can be seen in the options
output column of the command SHOW DATABASES
.
SHOW DATABASES yield name, options
name | options |
---|---|
|
|
|
|
|
|
|
|
Rows: 4 |
Remove the options set for a database
ALTER DATABASE `movies`
REMOVE OPTION txLogEnrichment
The REMOVE OPTION
clause removes the specified option from the database using the ALTER DATABASE
command.
SHOW DATABASES YIELD name, options
name | options |
---|---|
|
|
|
|
|
|
|
|
Rows: 4 |
Stop databases
Databases can be stopped using the command STOP DATABASE
.
STOP DATABASE customers
Both standard databases and composite databases can be stopped using this command. |
The status of the stopped database can be seen using the command SHOW DATABASE name
.
SHOW DATABASE customers YIELD name, requestedStatus, currentStatus
+-----------------------------------------------+ | name | requestedStatus | currentStatus | +-----------------------------------------------+ | "customers" | "offline" | "offline" | +-----------------------------------------------+
Databases that are stopped with the |
Start databases
Databases can be started using the command START DATABASE
.
START DATABASE customers
Both standard databases and composite databases can be started using this command. |
The status of the started database can be seen using the command SHOW DATABASE name
.
SHOW DATABASE customers YIELD name, requestedStatus, currentStatus
+-----------------------------------------------+ | name | requestedStatus | currentStatus | +-----------------------------------------------+ | "customers" | "online" | "online" | +-----------------------------------------------+
Delete databases
Databases can be deleted by using the command DROP DATABASE
.
Note that all database aliases must be dropped before dropping a database.
DROP DATABASE customers
Both standard databases and composite databases can be deleted using this command. |
The DROP DATABASE
command removes a database entirely.
Therefore, it no longer shows up in the listing provided by the command SHOW DATABASES
.
SHOW DATABASES YIELD name
+---------------------+ | name | +---------------------+ | "movies" | | "neo4j" | | "system" | +---------------------+
Use IF EXISTS
when deleting databases
The DROP DATABASE
command is optionally idempotent, with the default behavior to fail with an error if the database does not exist.
Appending IF EXISTS
to the command ensures that no error is returned and nothing happens should the database not exist.
It will always return an error if there is an existing alias that targets the database.
In that case, the alias needs to be dropped before dropping the database.
DROP DATABASE customers IF EXISTS
Use DUMP DATA
or DESTROY DATA
when deleting databases
You can request that a dump of the store files is produced first, and stored in the path configured using the dbms.directories.dumps.root
setting (by default <neo4j-home>/data/dumps
).
This can be achieved by appending DUMP DATA
to the command (or DESTROY DATA
to explicitly request the default behavior).
These dumps are equivalent to those produced by neo4j-admin dump
and can be similarly restored using neo4j-admin load
.
DROP DATABASE movies DUMP DATA
The options IF EXISTS
and DUMP DATA
/ DESTROY DATA
can also be combined.
An example could look like this:
DROP DATABASE customers IF EXISTS DUMP DATA
Wait options
The WAIT
sub-clause was added as an option to the ALTER DATABASE
command in Neo4j 5.7.
Aside from SHOW DATABASES
, all database management commands accept an optional WAIT
/NOWAIT
sub-clause.
The WAIT
/NOWAIT
sub-clause allows you to specify a time limit in which the command must complete and return.
The options are:
-
WAIT n SECONDS
- Returns once completed or when the specified time limit ofn
seconds is up. -
WAIT
- Returns once completed or when the default time limit of 300 seconds is up. -
NOWAIT
- Returns immediately.
A command using a WAIT
sub-clause automatically commits the current transaction when it executes successfully, as the command needs to run immediately for it to be possible to WAIT
for it to complete.
Any subsequent commands executed are therefore performed in a new transaction.
This is different from the usual transactional behavior, and for this reason, it is recommended that these commands be run in their own transaction.
The default behavior is NOWAIT
, so if no clause is specified the transaction behaves normally and the action is performed in the background post-commit.
A command with a |
WAIT
CREATE DATABASE slow WAIT 5 SECONDS
+-------------------------------------------------------+ | address | state | message | success | +-------------------------------------------------------+ | "localhost:7687" | "CaughtUp" | "caught up" | TRUE | +-------------------------------------------------------+
The success
column provides an aggregate status of whether or not the command is considered successful and thus every row will have the same value.
This column is to determine, for example in a script, whether or not the command has been completed successfully without timing out.