docs/versioned_docs/version-1.12.0/00200-core-concepts/00100-databases.md
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
A module is a collection of functions and schema definitions, which can be written in TypeScript, C# or Rust. Modules define the structure of your database and the server-side logic that processes and handles client requests.
A database is a running instance of a module. While a module is the code you write (schema and reducers), a database is the actual deployed entity running on a SpacetimeDB host with stored data and active connections.
Understanding this distinction is important:
You can deploy the same module to multiple databases (e.g. separate environments for testing, staging, production), each with its own independent data. When you update your module code and re-publish, SpacetimeDB will update the database's schema/logic — the existing data remains (though for complicated schema changes you may need to handle migrations carefully).
A module contains:
The logic is contained within these three categories of server-side functions: reducers (transactional state changes), procedures (functions with external capabilities), and views (read-only queries).
SpacetimeDB modules can be written in multiple languages:
<Tabs groupId="server-language" queryString> <TabItem value="typescript" label="TypeScript">TypeScript is fully supported for server modules. TypeScript is ideal for developers familiar with JavaScript/Node.js.
</TabItem> <TabItem value="csharp" label="C#">C# is fully supported for server modules. C# is an excellent choice for developers using Unity or .NET.
</TabItem> <TabItem value="rust" label="Rust">Rust is fully supported for server modules. Rust is a great choice for performance-critical applications.
When you publish a module, you give the database a name. Database names must match the regex /^[a-z0-9]+(-[a-z0-9]+)*$/, i.e. only lowercase ASCII letters and numbers, separated by dashes.
Examples of valid names:
my-game-serverchat-app-productiontest123Each database also receives a unique identity (a hex string) when created. Clients can connect using either the name or identity.
Modules and databases are administered using the spacetime CLI tool.
Create or update a database by publishing your module:
spacetime publish <DATABASE_NAME>
See spacetime publish for details on the publishing workflow.
When you republish to an existing database, SpacetimeDB attempts to automatically migrate the schema. For details on what changes are supported and migration strategies:
For all available publish options, see the spacetime publish CLI reference.
To permanently delete a database and all its data:
spacetime delete <DATABASE_NAME>
You'll be prompted to confirm the deletion. Use --yes to skip the confirmation in scripts.
:::warning Deleting a database is permanent and cannot be undone. All data will be lost. :::
For more options, see the spacetime delete CLI reference.
You can run SQL queries directly against your database:
spacetime sql <DATABASE_NAME> "SELECT * FROM user"
Important: When you run SQL queries as the database owner, you bypass table visibility restrictions. This means you can query private tables that normal clients cannot access.
To test queries as an unprivileged client would see them, use the --anonymous flag:
spacetime sql --anonymous <DATABASE_NAME> "SELECT * FROM user"
This executes the query as an anonymous client, respecting table visibility rules.
For more SQL options, see the spacetime sql CLI reference.
View logs from your database:
spacetime logs <DATABASE_NAME>
To stream logs as they're generated (similar to tail -f):
spacetime logs --follow <DATABASE_NAME>
This keeps the connection open and displays new log entries as they occur. Press Ctrl+C to stop following.
To view only the last N lines:
spacetime logs --num-lines 100 <DATABASE_NAME>
For more logging options, see the spacetime logs CLI reference.
To see all databases associated with your identity:
spacetime list
This shows database names, identities, and host servers.
You can also manage your databases through the SpacetimeDB web interface at spacetimedb.com:
:::tip The website provides a graphical interface for many CLI operations, making it easier to visualize and manage your databases. :::
SpacetimeDB supports organizing databases into projects and managing team access. This allows you to:
If you're new to SpacetimeDB, follow this recommended learning path:
spacetime init or spacetime devOnce you have the basics down, explore these essential topics:
Ready to level up? Dive into these advanced capabilities:
When you're ready to go live: