Unsupported versions: 7.0 / 6.5 / 6.4
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

CREATE DATABASE

Name

CREATE DATABASE — Creates a new database
CREATE DATABASE name [ WITH LOCATION = 'dbpath' ]
  

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.

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 CREATE USER.

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 'path'., 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.

Description

CREATE DATABASE creates a new Postgres 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 initlocation 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.

Notes

CREATE DATABASE is a Postgres 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.

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.
 
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
   

Compatibility

SQL92

There is no CREATE DATABASE statement in SQL92. Databases are equivalent to catalogs whose creation is implementation-defined.