CREATE DATABASE in Greenplum

CREATE DATABASE command is used to create a new database in Greenplum.

==================================================================================================================
Alter Database , Drop database
==================================================================================================================

CREATE DATABASE name [ [WITH] [OWNER [=] dbowner]
[TEMPLATE [=] template]
[ENCODING [=] encoding]
[TABLESPACE [=] tablespace]
[CONNECTION LIMIT [=] connlimit ] ]


CREATE DATABASE creates a new database. To create a database, you must be a superuser or have the special CREATEDB privilege.
The creator becomes the owner of the new database by default. Superusers can create databases owned by other users by 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.

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 clean database containing only the standard objects predefined by Greenplum Database. This is useful if you wish to avoid copying any installation-local objects that may have been added to template1.

Parameters 

name: The name of a database to create.
dbowner: The name of the database user who will own the new database, or DEFAULT to use the default owner (the user executing the command).
template: The name of the template from which to create the new database, or DEFAULT to use the default template (template1).
encoding: Character set encoding to use in the new database. Specify a string constant (such as 'SQL_ASCII'), an integer encoding number, or DEFAULT to use the default encoding
tablespace: The name of the tablespace that will be associated with the new database, or DEFAULT to use the template database’s tablespace. This tablespace will be the default tablespace used for objects created in this database.
connlimit: Warning: Setting a connection limit at the database level cannot be enforced in Greenplum Database and may cause queries to fail. Leave this set at the default of -1 (no limit). Connection limits may only be set at the system level in Greenplum Database.

Notes
CREATE DATABASE cannot be executed inside a transaction block.
When you copy a database by specifying its name as the template, no other sessions can be connected to the template database while it is being copied. New connections to the template database are locked out until CREATE DATABASE completes.

The CONNECTION LIMIT is not enforced against superusers.

Examples

To create a new database:
CREATE DATABASE gpdb;

To create a database sales owned by user salesapp with a default tablespace of salesspace:
CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;

To create a database music which supports the ISO-8859-1 character set:
CREATE DATABASE music ENCODING 'LATIN1';

Compatibility
There is no CREATE DATABASE statement in the SQL standard. Databases are equivalent to catalogs, whose creation is implementation-defined.


createdb command can also be used to create  a new database.

Usage
createdb [connection_option ...] [-D tablespace] [-E encoding]
[-O owner] [-T template] [-e] [dbname ['description']]
createdb --help | --version

Description
createdb creates a new database in a Greenplum Database system. Normally, the database user who executes this command becomes the owner of the new database. However a different owner can be specified via the -O option, if the executing user has appropriate privileges.

createdb is a wrapper around the SQL command CREATE DATABASE.

Options

dbname
The name of the database to be created. The name must be unique among all other databases in the Greenplum system. If not specified, reads from the environment variable PGDATABASE, then PGUSER or defaults to the current system user.

description:  A comment to be associated with the newly created database. Descriptions containing white space must be enclosed in quotes.
-D tablespace | --tablespace tablespace: The default tablespace for the database.
-e echo: Echo the commands that createdb generates and sends to the server.
-E encoding | --encoding encoding :Character set encoding to use in the new database. Specify a string constant (such as 'UTF8'), an integer encoding number, or DEFAULT to use the default encoding. 
-O owner | --owner owner: The name of the database user who will own the new database. Defaults to the user executing this command.
-T template | --template template: The name of the template from which to create the new database. Defaults to template1.

Connection Options: 

-h host | --host host : The host name of the machine on which the Greenplum master database server is running. If not specified, reads from the environment variable PGHOST or defaults to localhost.

-p port | --port port : The TCP port on which the Greenplum master database server is listening for connections. If not specified, reads from the environment variable PGPORT or defaults to 5432.

-U username | --username username : The database role name to connect as. If not specified, reads from the environment variable PGUSER or defaults to the current system role name.

-w | --no-password: Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.

-W | --password: Force a password prompt.

Examples

To create the database test using the default options:
createdb test

To create the database demo using the Greenplum master on host gpmaster, port 54321, using the LATIN1 encoding scheme:
createdb -p 54321 -h gpmaster -E LATIN1 demo


Comments