Cypher init script

Apoc optionally allows you to run cypher commands after database initialization is finished. This can e.g. be used to ensure indexes/constraints are created up front.

The initializers are defined by Configuration Options using the following naming convention:

apoc.initializer.<database_name>.<identifier> = <some cypher string>

For each database all initializer strings are ordered by <identifier> and each of them is executed in a separate transaction. If you only have one single initializer for a given database you can omit <identifier>.

As an example we want to

  • create another db user in system db

  • create a index for :Person in default db neo4j

  • add two person nodes in default db neo4j

This is achieved by

apoc.initializer.system=create user dummy set password 'abc'
apoc.initializer.neo4j.0=create index person_index for (p:Person) on (p.name)
apoc.initializer.neo4j.1=create (:Person{name:'foo'})
apoc.initializer.neo4j.2=create (:Person{name:'bar'})
There’s deprecated syntax originating back from the 3.x days - see below. This old style syntax will be removed in future. Note that the initializers in old syntax are applied to all databases (except system)
apoc.initializer.cypher=CALL apoc.cypher.runSchemaFile("file:///indexes.cypher")

For running multiple statements you can also add suffixes to the config options, like cypher.1, cypher.2. The statements will then be executed in sort-order.