5.5.2. User and role management

This section explains how to use Cypher to manage Neo4j role-based access control through users and roles.

5.5.2.1. User Management

Users can be created and managed using a set of Cypher administration commands executed against the system database.

Table 5.18. User management command syntax
Command Description Type of user Available in Community Edition
SHOW USERS

List all users

Admin

+

SHOW USER name PRIVILEGES

List the privileges granted to a user

Admin

-

CREATE [OR REPLACE] USER name [IF NOT EXISTS]
  SET PASSWORD password
  [[SET PASSWORD] CHANGE [NOT] REQUIRED]
  [SET STATUS {ACTIVE | SUSPENDED}]

Create a new user

Admin

+

ALTER USER name SET {
PASSWORD password
            [[SET PASSWORD] CHANGE [NOT] REQUIRED]
            [SET STATUS {ACTIVE | SUSPENDED} ] |
PASSWORD CHANGE [NOT] REQUIRED
            [SET STATUS {ACTIVE | SUSPENDED}] |
STATUS {ACTIVE | SUSPENDED}

Modify the settings for an existing user

Admin

-

ALTER CURRENT USER SET PASSWORD FROM original TO password

Change the current user’s password

Normal user

+

DROP USER name [IF EXISTS]

Drop (remove) an existing user

Admin

+

Listing users

Available users can be seen using SHOW USERS which will produce a table of users with four columns:

Table 5.19. List users output
Column Description Available in Community Edition

user

user name

+

roles

roles granted to the user

-

passwordChangeRequired

if true the user must change their password at next login

+

suspended

if true the user is currently suspended (cannot log in)

-

Query. 

SHOW USERS

Table 5.20. Result
user roles passwordChangeRequired suspended

1 row

"neo4j"

["admin"]

true

false

Try this query live.  none SHOW USERS

When first starting a Neo4j DBMS, there is always a single default user neo4j with administrative privileges. It is possible to set the initial password using neo4j-admin set-initial-password, otherwise it is necessary to change the password after first login.

The SHOW USER name PRIVILEGES command is described in Listing privileges.

Creating users

Users can be created using CREATE USER.

Command syntax. 

CREATE [OR REPLACE] USER name [IF NOT EXISTS]
      SET PASSWORD password
      [[SET PASSWORD] CHANGE [NOT] REQUIRED]
      [SET STATUS {ACTIVE | SUSPENDED}]

If the optional SET PASSWORD CHANGE [NOT] REQUIRED is omitted then the default is CHANGE REQUIRED. The default for SET STATUS is ACTIVE. The password can either be a string value or a string parameter.

For example, we can create the user jake in a suspended state and the requirement to change his password.

Query. 

CREATE USER jake
SET PASSWORD 'abc' CHANGE REQUIRED
SET STATUS SUSPENDED

0 rows, System updates: 1

The SUSPENDED flag is an enterprise feature.

Try this query live.  none CREATE USER jake SET PASSWORD 'abc' CHANGE REQUIRED SET STATUS SUSPENDED

The created user will appear on the list provided by SHOW USERS.

Query. 

SHOW USERS

Table 5.21. Result
user roles passwordChangeRequired suspended

2 rows

"neo4j"

["admin"]

true

false

"jake"

[]

true

true

Try this query live.  none SHOW USERS

When creating a user, their initial list of roles is empty. In Neo4j community-edition this is not important as all users have implied administator privileges. In Neo4j enterprise-edition a new user like this will have no rights and it is important to grant roles to the user.

The CREATE USER command is optionally idempotent, with the default behavior to throw an exception if the user already exists. Appending IF NOT EXISTS to the command will ensure that no exception is thrown and nothing happens should the user already exist. Adding OR REPLACE to the command will result in any existing user being deleted and a new one created.

Query. 

CREATE USER jake IF NOT EXISTS SET PASSWORD 'xyz'

0 rows

Try this query live.  none CREATE USER jake IF NOT EXISTS SET PASSWORD 'xyz'

Query. 

CREATE OR REPLACE USER jake
SET PASSWORD 'xyz'

0 rows, System updates: 2

This is equivalent to running DROP USER jake IF EXISTS followed by CREATE USER jake SET PASSWORD 'xyz'.

Try this query live.  none CREATE OR REPLACE USER jake SET PASSWORD 'xyz'

The IF NOT EXISTS and OR REPLACE parts of this command cannot be used together.

Modifying users

Users can be modified using ALTER USER.

Command syntax. 

ALTER USER name SET {
      PASSWORD password
            [[SET PASSWORD] CHANGE [NOT] REQUIRED]
            [SET STATUS {ACTIVE | SUSPENDED} ] |
      PASSWORD CHANGE [NOT] REQUIRED
            [SET STATUS {ACTIVE | SUSPENDED}] |
      STATUS {ACTIVE | SUSPENDED}
}

The password can either be a string value or a string parameter.

For example, we can modify the user jake with a new password and active status as well as remove the requirement to change his password.

Query. 

ALTER USER jake
SET PASSWORD 'abc123' CHANGE NOT REQUIRED
SET STATUS ACTIVE

0 rows, System updates: 1

Try this query live.  none ALTER USER jake SET PASSWORD 'abc123' CHANGE NOT REQUIRED SET STATUS ACTIVE

When altering a user it is only necessary to specify the changes required. For example, leaving out any STATUS change part of the query will leave that unchanged.

The changes to the user will appear on the list provided by SHOW USERS.

Query. 

SHOW USERS

Table 5.22. Result
user roles passwordChangeRequired suspended

2 rows

"neo4j"

["admin"]

true

false

"jake"

[]

false

false

Try this query live.  none SHOW USERS

Changing the current user’s password

Users can change their own password using ALTER CURRENT USER SET PASSWORD. The old password is required in addition to the new one, and either or both can be a string value or a string parameter. When a user executes this command it will change their password as well as set the CHANGE NOT REQUIRED flag.

Query. 

ALTER CURRENT USER
SET PASSWORD FROM 'abc123' TO '123xyz'

0 rows, System updates: 1

Try this query live.  none ALTER CURRENT USER SET PASSWORD FROM 'abc123' TO '123xyz'

This command only works for a logged in user and cannot be run with auth disabled.

Deleting users

Users can be deleted using DROP USER.

Query. 

DROP USER jake

0 rows, System updates: 1

Try this query live.  none DROP USER jake

When a user has been deleted, it will no longer appear on the list provided by SHOW USERS.

Query. 

SHOW USERS

Table 5.23. Result
user roles passwordChangeRequired suspended

1 row

"neo4j"

["admin"]

true

false

Try this query live.  none SHOW USERS

This command is optionally idempotent, with the default behavior to throw an exception if the user does not exists. Appending IF EXISTS to the command will ensure that no exception is thrown and nothing happens should the user not exist.

Query. 

DROP USER jake IF EXISTS

0 rows

Try this query live.  none DROP USER jake IF EXISTS

5.5.2.2. Role Management

Roles can be created and managed using a set of Cypher administration commands executed against the system database.

The role name PUBLIC is not permitted. This is a system-reserved role, and cannot be used.

Table 5.24. Role management command syntax
Command Description
SHOW [ALL|POPULATED] ROLES [WITH USERS]

List roles

SHOW ROLE name PRIVILEGES

List the privileges granted to a role

CREATE [OR REPLACE] ROLE name [IF NOT EXISTS] [AS COPY OF name]

Create a new role

DROP ROLE name [IF EXISTS]

Drop (remove) an existing role

GRANT ROLE name[, ...] TO user[, ...]

Assign one or multiple roles to one or multiple users

REVOKE ROLE name[, ...] FROM user[, ...]

Remove one or multiple roles from one or multiple users

Listing roles

Available roles can be seen using SHOW ROLES.

Query. 

SHOW ROLES

This is the same command as SHOW ALL ROLES. When first starting a Neo4j DBMS there are a number of built-in roles:

  • reader - can perform read-only queries on all databases except system.
  • editor - can perform read and write operations on all databases except system, but cannot make new labels or relationship types.
  • publisher - can do the same as editor, but also create new labels and relationship types.
  • architect - can do the same as publisher as well as create and manage indexes and constraints.
  • admin - can do the same as all the above, as well as manage databases, users, roles and privileges.

More information about the built-in roles can be found in Operations Manual → Built-in roles

Table 5.25. Result
role isBuiltIn

5 rows

"admin"

true

"publisher"

true

"editor"

true

"reader"

true

"architect"

true

Try this query live.  none SHOW ROLES

There are multiple versions of this command, the default being SHOW ALL ROLES. To only show roles that are assigned to users, the command is SHOW POPULATED ROLES. To see which users are assigned to roles WITH USERS can be appended to the commands. This will give one result row for each user, so if a role is assigned to two users then it will show up twice in the result.

Query. 

SHOW POPULATED ROLES
WITH USERS

The table of results will show information about the role and what database it belongs to.

Table 5.26. Result
role isBuiltIn member

1 row

"admin"

true

"neo4j"

Try this query live.  none SHOW POPULATED ROLES WITH USERS

The SHOW ROLE name PRIVILEGES command is found in Listing privileges.

Creating roles

Roles can be created using CREATE ROLE.

Query. 

CREATE ROLE myrole

0 rows, System updates: 1

Try this query live.  none CREATE ROLE myrole

A role can also be copied, keeping its privileges, using CREATE ROLE AS COPY OF.

Query. 

CREATE ROLE mysecondrole AS COPY OF myrole

0 rows, System updates: 1

Try this query live.  none CREATE ROLE mysecondrole AS COPY OF myrole

The created roles will appear on the list provided by SHOW ROLES.

Query. 

SHOW ROLES

Table 5.27. Result
role isBuiltIn

7 rows

"admin"

true

"publisher"

true

"editor"

true

"reader"

true

"architect"

true

"myrole"

false

"mysecondrole"

false

Try this query live.  none SHOW ROLES

These command versions are optionally idempotent, with the default behavior to throw an exception if the role already exists. Appending IF NOT EXISTS to the command will ensure that no exception is thrown and nothing happens should the role already exist. Adding OR REPLACE to the command will result in any existing role being deleted and a new one created.

Query. 

CREATE ROLE myrole IF NOT EXISTS

0 rows

Try this query live.  none CREATE ROLE myrole IF NOT EXISTS

Query. 

CREATE OR REPLACE ROLE myrole

0 rows, System updates: 2

This is equivalent to running DROP ROLE myrole IF EXISTS followed by CREATE ROLE myrole.

Try this query live.  none CREATE OR REPLACE ROLE myrole

The IF NOT EXISTS and OR REPLACE parts of this command cannot be used together.

Deleting roles

Roles can be deleted using DROP ROLE command.

Query. 

DROP ROLE mysecondrole

0 rows, System updates: 1

Try this query live.  none DROP ROLE mysecondrole

When a role has been deleted, it will no longer appear on the list provided by SHOW ROLES.

Query. 

SHOW ROLES

Table 5.28. Result
role isBuiltIn

5 rows

"admin"

true

"publisher"

true

"editor"

true

"reader"

true

"architect"

true

Try this query live.  none SHOW ROLES

This command is optionally idempotent, with the default behavior to throw an exception if the role does not exists. Appending IF EXISTS to the command will ensure that no exception is thrown and nothing happens should the role not exist.

Query. 

DROP ROLE mysecondrole IF EXISTS

0 rows

Try this query live.  none DROP ROLE mysecondrole IF EXISTS

Assigning roles to users

Users can be given access rights by assigning them roles using GRANT ROLE.

Query. 

GRANT ROLE myrole TO jake

0 rows, System updates: 1

Try this query live.  none GRANT ROLE myrole TO jake

The roles assigned to each user can be seen in the list provided by SHOW USERS.

Query. 

SHOW USERS

Table 5.29. Result
user roles passwordChangeRequired suspended

5 rows

"neo4j"

["admin"]

true

false

"jake"

["myrole"]

false

false

"user1"

[]

true

false

"user2"

[]

true

false

"user3"

[]

true

false

Try this query live.  none SHOW USERS

It is possible to assign multiple roles to multiple users in one command.

Query. 

GRANT ROLES role1, role2 TO user1, user2, user3

0 rows, System updates: 6

Try this query live.  none GRANT ROLES role1, role2 TO user1, user2, user3

Query. 

SHOW USERS

Table 5.30. Result
user roles passwordChangeRequired suspended

5 rows

"neo4j"

["admin"]

true

false

"jake"

["myrole"]

false

false

"user1"

["role1","role2"]

true

false

"user2"

["role2","role1"]

true

false

"user3"

["role2","role1"]

true

false

Try this query live.  none SHOW USERS

Revoking roles from users

Users can lose access rights by revoking roles from them using REVOKE ROLE.

Query. 

REVOKE ROLE myrole FROM jake

0 rows, System updates: 1

Try this query live.  none REVOKE ROLE myrole FROM jake

The roles revoked from users can no longer be seen in the list provided by SHOW USERS.

Query. 

SHOW USERS

Table 5.31. Result
user roles passwordChangeRequired suspended

5 rows

"neo4j"

["admin"]

true

false

"jake"

[]

false

false

"user1"

["role1","role2"]

true

false

"user2"

["role2","role1"]

true

false

"user3"

["role2","role1"]

true

false

Try this query live.  none SHOW USERS

It is possible to revoke multiple roles from multiple users in one command.

Query. 

REVOKE ROLES role1, role2 FROM user1, user2, user3

0 rows, System updates: 6

Try this query live.  none REVOKE ROLES role1, role2 FROM user1, user2, user3