Reference: Databases
A database is a named, logically separate collection of schemas, tables, and other objects on a CedarDB server. When you connect to CedarDB, you choose the database to connect to. All queries on that connection can only read and write objects in that database. Roles are shared by all databases on the server.
CREATE DATABASE forestry;Afterward, connect to the new database:
psql -h localhost -U postgres -d forestryCREATE DATABASE
CREATE DATABASE creates a new, empty database.
CREATE DATABASE <name>
[ [ WITH ] [ OWNER [=] <role_name> ]
[ ENCODING [=] 'UTF8' ]
[ TEMPLATE [=] { template0 | template1 | DEFAULT } ] ]| Parameter | Description |
|---|---|
<name> | The name of the new database. |
OWNER | The role that owns the new database. Defaults to the role that runs the statement. |
ENCODING | The character encoding. CedarDB always stores text as UTF-8. |
TEMPLATE | Accepted for compatibility with template0, template1, and DEFAULT only. The new database is always empty. |
Create a database that belongs to another role:
CREATE ROLE forester LOGIN;
CREATE DATABASE forestry_reports OWNER forester;SELECT datname, datdba::regrole AS owner FROM pg_database WHERE datname = 'forestry_reports'; datname | owner
------------------+----------
forestry_reports | foresterPermissions
To create a database, you must be a superuser or have the CREATEDB attribute.
The creator owns the new database, unless you specify OWNER.
With OWNER, you must be able to SET ROLE to the new owner, i.e., be a member of that role with the SET option.
ALTER DATABASE
ALTER DATABASE renames a database, changes its owner, or sets default values for configuration parameters.
ALTER DATABASE <name> RENAME TO <new_name>
ALTER DATABASE <name> OWNER TO { <new_owner> | CURRENT_USER | CURRENT_ROLE | SESSION_USER }
ALTER DATABASE <name> SET <parameter> { TO | = } { <value> | DEFAULT }
ALTER DATABASE <name> RESET { <parameter> | ALL }Rename a database
You cannot rename a database while any session, including your own, is connected to it:
CREATE DATABASE forestry;
ALTER DATABASE forestry RENAME TO forestry_archive;Set a default for a configuration parameter
The default applies to every new session that connects to the database.
It does not affect sessions that are already running. Use an explicit SET for those.
Role defaults for the same parameter override the database default:
CREATE DATABASE forestry;
ALTER DATABASE forestry SET search_path = 'inventory';SELECT setconfig FROM pg_db_role_setting; setconfig
-------------------------
{search_path=inventory}Remove the default again with RESET:
ALTER DATABASE forestry RESET search_path;
ALTER DATABASE forestry RESET ALL;Permissions
To alter a database, you must own it or be a superuser. In addition:
RENAME TOrequires theCREATEDBattribute.OWNER TOrequires that you have theCREATEDBattribute and are a member of the new owner role with theSEToption.
Superusers can always rename a database and change its owner.
DROP DATABASE
DROP DATABASE removes a database and all objects in it.
This cannot be undone after the transaction executing it commited.
DROP DATABASE [ IF EXISTS ] <name>CREATE DATABASE forestry;
DROP DATABASE forestry;Use IF EXISTS to avoid an error if the database does not exist:
DROP DATABASE IF EXISTS forestry;You cannot drop the database you are connected to, or a database that other sessions are still connected to.
Permissions
To drop a database, you must own it or be a superuser.
Database privileges
Database privileges control who can connect to a database and what they can create in it.
| Privilege | Allows |
|---|---|
CONNECT | Connecting to the database. |
CREATE | Creating schemas in the database. |
TEMPORARY, TEMP | Creating temporary tables and other temporary objects in the database. |
ALL [PRIVILEGES] | All of the above. |
By default, every role can connect to a database and create temporary objects in it, because PUBLIC holds CONNECT and TEMPORARY.
Only the owner can create schemas in it.
GRANT and REVOKE require an enterprise license.Restrict a database to specific roles by revoking CONNECT from PUBLIC and granting it again to individual roles:
CREATE DATABASE forestry;
CREATE ROLE forester LOGIN;
REVOKE CONNECT ON DATABASE forestry FROM PUBLIC;
GRANT CONNECT ON DATABASE forestry TO forester;Other roles now fail to connect with FATAL: user not permitted to connect to database.
Without an enterprise license, CedarDB still enforces privileges that were granted earlier.
For the full GRANT and REVOKE syntax, see Roles.
PostgreSQL Differences
- CedarDB does not support the
LOCALE,LC_COLLATE,LC_CTYPE,LOCALE_PROVIDER,ICU_LOCALE,COLLATION_VERSION,STRATEGY,OID,TABLESPACE,CONNECTION LIMIT,IS_TEMPLATE, andALLOW_CONNECTIONSoptions ofCREATE DATABASE. Use theCOLLATEspecifier for per-column collations. TEMPLATEonly acceptstemplate0,template1, andDEFAULT. CedarDB has no template databases, and new databases are always empty. Other template names fail withdatabase templates not supported.ENCODINGrejects encodings other than UTF-8, such asLATIN1.ALTER DATABASEdoes not supportSET TABLESPACE,SET <parameter> FROM CURRENT,REFRESH COLLATION VERSION, or theWITHoptionsCONNECTION LIMIT,ALLOW_CONNECTIONS, andIS_TEMPLATE.- Per-role defaults for a single database (
ALTER ROLE ... IN DATABASE ... SET) are not supported. DROP DATABASEdoes not supportWITH (FORCE).CREATE DATABASEandDROP DATABASEare allowed inside a transaction block. AROLLBACKundoes them.COMMENT ON DATABASEis not supported.