Alter databases

You can modify standard databases using the Cypher command ALTER DATABASE.

Syntax

Command Syntax

ALTER DATABASE

ALTER DATABASE name [IF EXISTS]
{
SET ACCESS {READ ONLY | READ WRITE} |
SET TOPOLOGY n PRIMAR{Y|IES} [m SECONDAR{Y|IES}] |
SET OPTION option value
}
[WAIT [n [SEC[OND[S]]]]|NOWAIT]
ALTER DATABASE name [IF EXISTS]
REMOVE OPTION option
[WAIT [n [SEC[OND[S]]]]|NOWAIT]

There can be multiple SET OPTION or REMOVE OPTION clauses for different option keys.

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 settings server.databases.default_to_read_only, server.databases.read_only, and server.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.

The WAIT sub-clause was added as an option to the ALTER DATABASE command in Neo4j 5.7.

Modifying access mode is only available to standard databases and not composite databases.

Alter database access mode to read-only

To modify the database access mode, use the following command where customers is the database name:

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
Result
+----------------------------+
| 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 if the database does 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

txLogEnrichment

FULL|DIFF|OFF

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 → Set the enrichment mode.

There are no available OPTIONS values for composite databases.

Modify 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
Table 1. Result
name options

"customers"

{}

"movies"

{txLogEnrichment: "FULL"}

"neo4j"

{}

"system"

{}

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
Table 2. Result
name options

"customers"

{}

"movies"

{}

"neo4j"

{}

"system"

{}

Rows: 4