NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›

Databases

One server holds several databases, each with its own tables, roles and transaction log. This page covers creating, listing, selecting and dropping them, and what belongs to one database rather than to the whole server.

The default database

Every server has a default database, named by --pg-default-db (postgres unless you change it). It is stored in the --location directory itself, and it is the database a connection gets when it names none. It always exists and cannot be dropped.

Every other database is a subdirectory of the data directory with the database's name:

text
/var/lib/rasterizeddb/        the default database, "postgres"
  analytics/                  the database "analytics"
  staging/                    the database "staging"

The server finds its databases by looking at these subdirectories when it starts; there is no separate list to keep in step.

Creating a database

sql
CREATE DATABASE analytics;
CREATE DATABASE IF NOT EXISTS analytics;

CREATE DATABASE needs a superuser. IF NOT EXISTS makes it a no-op when the database is already there, which migration tools rely on.

Names follow these rules, and an unquoted name is folded to lower case:

  • letters, digits and underscores only;
  • not starting with a digit;
  • at most 63 characters.
SQLSTATEMeaning
42P04A database with that name already exists.
42602The name breaks the rules above.
42501The role is not allowed to create databases.
58000The server could not create the directory.

CREATE DATABASE takes no other options (ENCODING, LOCALE and the like are refused rather than ignored). Text is always UTF-8.

Who can log in to a new database

Roles are per database, so a new database needs at least one role before anyone can connect to it.

  • The new database starts with the role that created it and, if given, its OWNER, each with its password. SET CONFIG roles.create_database_roles = all copies every role of the default database instead. A role with the CREATEDB attribute may create databases without being a superuser.
  • Before 1.1.0 a new database started with only the bootstrap role (--pg-user, default rasterized) and the password the server was started with.
sql
CREATE DATABASE analytics OWNER app;
CREATE DATABASE analytics WITH OWNER = app;

See Security.

Copying a database

CREATE DATABASE <name> TEMPLATE <source> makes a copy of an existing database, as a branch of it. template0 and template1, PostgreSQL's empty templates, give an ordinary empty database. Branches can also be reset, promoted, compared and merged back; see Branching.

sql
CREATE DATABASE staging TEMPLATE postgres;

Listing databases

sql
SHOW DATABASES;
text
 database_name |            location             | tables | sessions | is_default | is_current
---------------+---------------------------------+--------+----------+------------+------------
 postgres      | /var/lib/rasterizeddb           |     12 |        3 | t          | t
 analytics     | /var/lib/rasterizeddb/analytics |      4 |        0 | f          | f
ColumnMeaning
database_nameThe name.
locationIts directory.
tablesHow many tables it holds.
sessionsHow many sessions are connected to it.
is_defaultWhether it is the default database.
is_currentWhether it is the one this session is connected to.

The PostgreSQL catalogue view works too, for tools that read it:

sql
SELECT datname FROM pg_database ORDER BY datname;

Selecting a database

A connection chooses its database when it connects, and keeps it for its whole life. A name that does not exist is refused with SQLSTATE 3D000.

ClientHow
psqlpsql "host=db.example.com user=rasterized dbname=analytics sslmode=require"
libpq URL, JDBC, sqlxthe path: postgresql://db.example.com:5432/analytics
NpgsqlDatabase=analytics
rasterizeddb_clientOptions::database; see Rust client: connecting
Studiothe database field of the connection
MySQL clientsthe database argument (mysql … analytics), or USE analytics outside a transaction
SQL Server clients-d analytics or Database=analytics; master, model, msdb and tempdb mean the default database

To work in another database, open another connection. In psql, \c analytics does that.

sql
SELECT current_database();

Dropping a database

sql
DROP DATABASE staging;
DROP DATABASE IF EXISTS staging;

DROP DATABASE needs a superuser and removes the database's directory and everything in it. It is refused, with SQLSTATE 55006, when:

  • it is the default database;
  • it is the database the session is connected to (connect to another one first);
  • other sessions are connected to it.

IF EXISTS makes dropping a missing database a no-op.

What belongs to one database

Per databaseFor the whole server
Tables, indexes, views, sequences, functions, triggersListeners, ports, TLS and authentication methods
Roles and their passwords, grants and policiesThe settings file (rasterizeddb.conf) and most SET CONFIG keys
Its write-ahead log and transactionsThe data-directory lock
SET CONFIG replication.* and vacuum.enabledWorker threads, memory and cache settings
Its key/value cache store (KV … statements)

A transaction runs in one database: there is no query or transaction across two. A replicating server replicates every database, and a CREATE DATABASE on one node is created on its peers; see Replication.

Limits

  • No cross-database queries or transactions.
  • ALTER DATABASE (renaming a database, setting its options) is not supported.
  • The default database cannot be dropped.
  • CREATE DATABASE options other than TEMPLATE and OWNER are refused.
✓ page rendered in 264 µs axum + tera + htmx · no other javascript