Skip to main content
Version: 1.x

DEFINE DATABASE statement

The DEFINE DATABASE statement allows you to instantiate a named database, enabling you to specify security and configuration options.

Requirements

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

Statement syntax

SurrealQL Syntax
DEFINE DATABASE [ IF NOT EXISTS ] @name [CHANGEFEED @duration | @versionstamp [INCLUDE ORIGINAL] ] [ COMMENT @string ]

Example usage

Below shows how you can create a database using the DEFINE DATABASE statement.

-- Specify the namespace for the database
USE NS abcum;
-- Define database
DEFINE DATABASE app_vitalsense;

Using IF NOT EXISTS clause Since 1.3.0

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

-- Create a DATABASE if it does not already exist
DEFINE DATABASE IF NOT EXISTS app_vitalsense;

Changefeeds

A changefeed can be defined on a database, in which case all table events will show up on the changefeed without needing to be included in a DEFINE TABLE statement.

DEFINE DATABASE my_database CHANGEFEED 3d;

CREATE reading SET story = "Once upon a time";
CREATE reading SET story = "there was a database";
CREATE company SET name = "SurrealDB", stories = (SELECT * FROM story);
UPDATE reading SET story = story + "!";

SHOW CHANGES FOR TABLE reading SINCE 1 LIMIT 10;
SHOW CHANGES FOR TABLE company SINCE 1 LIMIT 10;