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:
/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
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.
| SQLSTATE | Meaning |
|---|---|
42P04 | A database with that name already exists. |
42602 | The name breaks the rules above. |
42501 | The role is not allowed to create databases. |
58000 | The 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 = allcopies every role of the default database instead. A role with theCREATEDBattribute may create databases without being a superuser. - Before 1.1.0 a new database started with only the bootstrap role (
--pg-user, defaultrasterized) and the password the server was started with.
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.
CREATE DATABASE staging TEMPLATE postgres;Listing databases
SHOW DATABASES; 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| Column | Meaning |
|---|---|
database_name | The name. |
location | Its directory. |
tables | How many tables it holds. |
sessions | How many sessions are connected to it. |
is_default | Whether it is the default database. |
is_current | Whether it is the one this session is connected to. |
The PostgreSQL catalogue view works too, for tools that read it:
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.
| Client | How |
|---|---|
psql | psql "host=db.example.com user=rasterized dbname=analytics sslmode=require" |
| libpq URL, JDBC, sqlx | the path: postgresql://db.example.com:5432/analytics |
| Npgsql | Database=analytics |
rasterizeddb_client | Options::database; see Rust client: connecting |
| Studio | the database field of the connection |
| MySQL clients | the 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.
SELECT current_database();Dropping a database
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 database | For the whole server |
|---|---|
| Tables, indexes, views, sequences, functions, triggers | Listeners, ports, TLS and authentication methods |
| Roles and their passwords, grants and policies | The settings file (rasterizeddb.conf) and most SET CONFIG keys |
| Its write-ahead log and transactions | The data-directory lock |
SET CONFIG replication.* and vacuum.enabled | Worker 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 DATABASEoptions other thanTEMPLATEandOWNERare refused.