Skip to main content

DEFINE NAMESPACE statement

SurrealDB has a multi-tenancy model which allows you to scope databases to a namespace. There is no limit to the number of databases that can be in a namespace, nor is there a limit to the number of namespaces allowed. Only users with root access are authorized to create namespaces.

Let's say that you're using SurrealDB to create a multi-tenant SaaS application. You can guarantee that the data of each tenant will be kept separate from other tenants if you put each tenant's databases into separate namespaces. In other words, this will ensure that information will remain siloed so user will only have access the information in the namespace they are a member of.

Requirements

  • You must be authenticated as a root or namespace user before you can use the DEFINE NAMESPACE statement.

Statement syntax

SurrealQL Syntax
DEFINE NAMESPACE [ IF NOT EXISTS ] @name

Example usage

Below shows how you can create a namespace using the DEFINE NAMESPACE statement.

-- Namespace for Abcum Ltd.
DEFINE NAMESPACE abcum;

Using IF NOT EXISTS clause Since 1.3.0

The IF NOT EXISTS clause can be used to define a namespace only if it does not already exist. If the namespace already exists, the DEFINE NAMESPACE statement will return an error.

-- Create a NAMESPACE if it does not already exist
DEFINE NAMESPACE IF NOT EXISTS example;