CREATE DATABASE
SQL - Language Statements
CREATE DATABASE
Creates a new database
1998-04-15
CREATE DATABASE name [WITH LOCATION = 'dbpath']
1998-04-15
Inputs
name
The name of a database to create.
dbpath
An alternate location can be specified as either an
environment variable known to the backend server
(e.g. 'PGDATA2') or as an absolute path name
(e.g. '/usr/local/pgsql/data').
In either case, the location must be pre-configured
by initlocation.
1998-04-15
Outputs
CREATEDB
Message returned if the command completes successfully.
WARN: createdb: database "name" already exists.
This occurs if database specified already exists.
ERROR: Unable to create database directory directory
There was a problem with creating the required directory; this operation will
need permissions for the postgres user on the specified location.
1998-04-15
Description
CREATE DATABASE creates a new PostgreSQL database.
The creator becomes the administrator of the new database.
1998-04-15
Notes
CREATE DATABASE statement is a PostgreSQL language extension.
Refer to DROP DATABASE statement to remove a database.
Usage
To create a new database:
CREATE DATABASE lusiadas;
To create a new database in ~/private_db:
$ mkdir private_db
$ initlocation ~/private_db
Creating Postgres database system directory /home/olly/private_db/base
$ chmod a+rx private_db
$ chmod a+rwx private_db/base
$ psql
Welcome to the POSTGRESQL interactive sql monitor:
Please read the file COPYRIGHT for copyright terms of POSTGRESQL
type \? for help on slash commands
type \q to quit
type \g or terminate with semicolon to execute query
You are currently connected to the database: template1
template1=> create database elsewhere with location = '/home/olly/private_db';
CREATEDB
Bugs
Where an ordinary user cannot change the ownership of files to other users
(which is correct from a security point of view), there is no easy way
to give the postgres backend access to files in a database at a
non-standard location without
giving similar access to all users. The situation shown in the usage
example makes /home/olly/private_db world-accessible.
If, on the other hand, the user gets the system administrator to give
file ownership to the postgres superuser and restrict access, he will
then be unable to delete the directory/base directory.
initlocation does not create a PG_VERSION file in the specified location.
How will PostgreSQL handle the situation if it is upgraded to an
incompatible database version?
Compatibility
1998-04-15
SQL92
There is no CREATE DATABASE statement on SQL92.
The equivalent command in standard SQL is CREATE SCHEMA.