Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

CREATE DATABASE

CREATE DATABASE creates and initializes an independent physical store in the current cluster:

CREATE DATABASE name;
CREATE DATABASE IF NOT EXISTS name;
CREATE DATABASE name TEMPLATE template1;
ALTER DATABASE name OWNER TO role;
DROP DATABASE name;
DROP DATABASE IF EXISTS name;

Only an administrator can run the command in this delivery. The directory is created at databases/<name> with a control file, WAL, and bootstrapped catalog. IF NOT EXISTS emits a notice when the directory already exists.

Bootstrap creates amphi and template1. Without TEMPLATE, the source is template1; TEMPLATE name clones the selected database’s catalog and data. Templates cannot have active connections.

Roles, SCRAM credentials, and group memberships are cluster-global: changes made from amphi or a child database are resolved through the cluster authority and are available for authentication in the other databases. The command does not switch the current session’s database. New connections can provide the created name in startup and the server opens and caches the matching store. The server also discovers valid physical stores in the cluster. \\l lists those names through sys.catalog_databases, which also exposes owner and template. The sys.catalog_roles, sys.catalog_groups, and sys.catalog_group_members projections also reflect the global authority. The custom dump preserves SCRAM verifiers; the SQL/plain format preserves only role attributes and memberships.

The cluster dump preserves database owners and TEMPLATE sources in dependency order. template0 is intentionally not part of the product.

Main errors are 3D000 for an unknown database, 42501 for a non-administrator, 42P04 for a duplicate name without IF NOT EXISTS, and 42602 for an invalid name.

ALTER DATABASE requires an administrator and an existing role. DROP DATABASE requires an administrator, refuses the current database, and removes the store files. The command also refuses the operation while the target database has active connections.