CREATE DATABASE
SQL - Language Statements
CREATE DATABASE
Creates a new database
1999-12-11
CREATE DATABASE name [ WITH LOCATION = 'dbpath' ]
1999-12-11
Inputs
name
The name of a database to create.
dbpath
An alternate location where to store the new database in the filesystem.
See below for caveats.
1999-12-11
Outputs
CREATE DATABASE
Message returned if the command completes successfully.
ERROR: user 'username' is not allowed to create/drop databases
You must have the special CREATEDB privilege to create databases.
See .
ERROR: createdb: database "name" already exists
This occurs if a database with the name
specified already exists.
ERROR: Single quotes are not allowed in database names.
ERROR: Single quotes are not allowed in database paths.
The database name and
dbpath cannot contain
single quotes. This is required so that the shell commands that
create the database directory can execute safely.
ERROR: The path 'xxx' is invalid.
The expansion of the specified dbpath
(see below how) failed. Check the path you entered or make sure that the
environment variable you are referencing does exist.
ERROR: createdb: May not be called in a transaction block.
If you have an explicit transaction block in progress you cannot call
CREATE DATABASE. You must finish the transaction first.
ERROR: Unable to create database directory 'xxx'.
ERROR: Could not initialize database directory.
These are most likely related to insufficient permissions on the data
directory, a full disk, or other file system problems. The user under
which the database server is running, must have access to the location.
1999-12-11
Description
CREATE DATABASE creates a new
PostgreSQL database.
The creator becomes the owner of the new database.
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
command.
If the path contains a slash, the leading part is interpreted
as an environment variable, which must be known to the
server process. This way the database administrator can
exercise control over at which locations 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.
1999-12-11
Notes
CREATE DATABASE is a Postgres
language extension.
Use to remove a database.
The program 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.
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
Creating Postgres database system directory /home/olly/private_db/base
$ psql olly
Welcome to psql, the PostgreSQL interactive terminal.
(Please type \copyright to see the distribution terms of PostgreSQL.)
Type \h for help with SQL commands,
\? for help on internal slash commands,
\q to quit,
\g or terminate with semicolon to execute query.
olly=> CREATE DATABASE elsewhere WITH LOCATION = '/home/olly/private_db';
CREATE DATABASE
Compatibility
1998-04-15
SQL92
There is no CREATE DATABASE statement in SQL92.
The equivalent command in standard SQL is CREATE SCHEMA.