Description
CREATE DATABASE creates a new
PostgreSQL database.
Normally, the creator becomes the owner of the new database.
Superusers can create databases owned by other users using the
OWNER clause. They can even create databases owned by
users with no special privileges. Non-superusers with CREATEDB
privilege can only create databases owned by themselves.
An alternate location can be specified in order to,
for example, store the database on a different disk.
The path must have been prepared with the
initlocation
command.
If the path name does not contain a slash, it is interpreted
as an environment variable name, which must be known to the
server process. This way the database administrator can
exercise control over locations in which databases can be created.
(A customary choice is, e.g., PGDATA2.)
If the server is compiled with ALLOW_ABSOLUTE_DBPATHS
(not so by default), absolute path names, as identified by
a leading slash
(e.g., /usr/local/pgsql/data),
are allowed as well.
By default, the new database will be created by cloning the standard
system database template1. A different template can be
specified by writing TEMPLATE =
name. In particular,
by writing TEMPLATE = template0, you can create a virgin
database containing only the standard objects predefined by your
version of PostgreSQL. This is useful
if you wish to avoid copying
any installation-local objects that may have been added to
template1.
The optional encoding parameter allows selection of the database encoding,
if your server was compiled with multibyte encoding support. When not
specified, it defaults to the encoding used by the selected template
database.
Optional parameters can be written in any order, not only the order
illustrated above.
Notes
CREATE DATABASE is a PostgreSQL
language extension.
Use DROP DATABASE to remove a database.
The program createdb is a
shell script wrapper around this command, provided for convenience.
There are security and data integrity issues
involved with using alternate database locations
specified with absolute path names, and by default
only an environment variable known to the backend may be
specified for an alternate location.
See the Administrator's Guide for more information.
Although it is possible to copy a database other than template1
by specifying its name as the template, this is not (yet) intended as
a general-purpose COPY DATABASE facility.
We recommend that databases used as templates be treated as read-only.
See the Administrator's Guide for more information.
Usage
To create a new database:
olly=> create database lusiadas;
To create a new database in an alternate area ~/private_db:
$ mkdir private_db
$ initlocation ~/private_db
The location will be initialized with username "olly".
This user will own all the files and must also own the server process.
Creating directory /home/olly/private_db
Creating directory /home/olly/private_db/base
initlocation is complete.
$ psql olly
Welcome to psql, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
olly=> CREATE DATABASE elsewhere WITH LOCATION = '/home/olly/private_db';
CREATE DATABASE