<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >CREATE DATABASE</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 8.3.6 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="CREATE CONVERSION" HREF="sql-createconversion.html"><LINK REL="NEXT" TITLE="CREATE DOMAIN" HREF="sql-createdomain.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2009-02-03T04:34:16"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 8.3.6 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-createconversion.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-createconversion.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-createdomain.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-createdomain.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-CREATEDATABASE" ></A >CREATE DATABASE</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN51965" ></A ><H2 >Name</H2 >CREATE DATABASE -- create a new database</DIV ><A NAME="AEN51968" ></A ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN51970" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >CREATE DATABASE <TT CLASS="REPLACEABLE" ><I >name</I ></TT > [ [ WITH ] [ OWNER [=] <TT CLASS="REPLACEABLE" ><I >dbowner</I ></TT > ] [ TEMPLATE [=] <TT CLASS="REPLACEABLE" ><I >template</I ></TT > ] [ ENCODING [=] <TT CLASS="REPLACEABLE" ><I >encoding</I ></TT > ] [ TABLESPACE [=] <TT CLASS="REPLACEABLE" ><I >tablespace</I ></TT > ] [ CONNECTION LIMIT [=] <TT CLASS="REPLACEABLE" ><I >connlimit</I ></TT > ] ]</PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN51978" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >CREATE DATABASE</TT > creates a new <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > database. </P ><P > To create a database, you must be a superuser or have the special <TT CLASS="LITERAL" >CREATEDB</TT > privilege. See <A HREF="sql-createuser.html" ><I >CREATE USER</I ></A >. </P ><P > Normally, the creator becomes the owner of the new database. Superusers can create databases owned by other users, by using the <TT CLASS="LITERAL" >OWNER</TT > clause. They can even create databases owned by users with no special privileges. Non-superusers with <TT CLASS="LITERAL" >CREATEDB</TT > privilege can only create databases owned by themselves. </P ><P > By default, the new database will be created by cloning the standard system database <TT CLASS="LITERAL" >template1</TT >. A different template can be specified by writing <TT CLASS="LITERAL" >TEMPLATE <TT CLASS="REPLACEABLE" ><I >name</I ></TT ></TT >. In particular, by writing <TT CLASS="LITERAL" >TEMPLATE template0</TT >, you can create a virgin database containing only the standard objects predefined by your version of <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN >. This is useful if you wish to avoid copying any installation-local objects that might have been added to <TT CLASS="LITERAL" >template1</TT >. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN51996" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="REPLACEABLE" ><I >name</I ></TT ></DT ><DD ><P > The name of a database to create. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >dbowner</I ></TT ></DT ><DD ><P > The name of the database user who will own the new database, or <TT CLASS="LITERAL" >DEFAULT</TT > to use the default (namely, the user executing the command). </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >template</I ></TT ></DT ><DD ><P > The name of the template from which to create the new database, or <TT CLASS="LITERAL" >DEFAULT</TT > to use the default template (<TT CLASS="LITERAL" >template1</TT >). </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >encoding</I ></TT ></DT ><DD ><P > Character set encoding to use in the new database. Specify a string constant (e.g., <TT CLASS="LITERAL" >'SQL_ASCII'</TT >), or an integer encoding number, or <TT CLASS="LITERAL" >DEFAULT</TT > to use the default encoding (namely, the encoding of the template database). The character sets supported by the <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > server are described in <A HREF="multibyte.html#MULTIBYTE-CHARSET-SUPPORTED" >Section 22.2.1</A >. See below for additional restrictions. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >tablespace</I ></TT ></DT ><DD ><P > The name of the tablespace that will be associated with the new database, or <TT CLASS="LITERAL" >DEFAULT</TT > to use the template database's tablespace. This tablespace will be the default tablespace used for objects created in this database. See <A HREF="sql-createtablespace.html" ><I >CREATE TABLESPACE</I ></A > for more information. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >connlimit</I ></TT ></DT ><DD ><P > How many concurrent connections can be made to this database. -1 (the default) means no limit. </P ></DD ></DL ></DIV ><P > Optional parameters can be written in any order, not only the order illustrated above. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN52039" ></A ><H2 >Notes</H2 ><P > <TT CLASS="COMMAND" >CREATE DATABASE</TT > cannot be executed inside a transaction block. </P ><P > Errors along the line of <SPAN CLASS="QUOTE" >"could not initialize database directory"</SPAN > are most likely related to insufficient permissions on the data directory, a full disk, or other file system problems. </P ><P > Use <A HREF="sql-dropdatabase.html" ><I >DROP DATABASE</I ></A > to remove a database. </P ><P > The program <A HREF="app-createdb.html" ><I ><I >createdb</I ></I ></A > is a wrapper program around this command, provided for convenience. </P ><P > Although it is possible to copy a database other than <TT CLASS="LITERAL" >template1</TT > by specifying its name as the template, this is not (yet) intended as a general-purpose <SPAN CLASS="QUOTE" >"<TT CLASS="COMMAND" >COPY DATABASE</TT >"</SPAN > facility. The principal limitation is that no other sessions can be connected to the template database while it is being copied. <TT CLASS="COMMAND" >CREATE DATABASE</TT > will fail if any other connection exists when it starts; otherwise, new connections to the template database are locked out until <TT CLASS="COMMAND" >CREATE DATABASE</TT > completes. See <A HREF="manage-ag-templatedbs.html" >Section 20.3</A > for more information. </P ><P > Any character set encoding specified for the new database must be compatible with the server's <TT CLASS="ENVAR" >LC_CTYPE</TT > locale setting. If <TT CLASS="ENVAR" >LC_CTYPE</TT > is <TT CLASS="LITERAL" >C</TT > (or equivalently <TT CLASS="LITERAL" >POSIX</TT >), then all encodings are allowed, but for other locale settings there is only one encoding that will work properly, and so the apparent freedom to specify an encoding is illusory if you didn't initialize the database cluster in <TT CLASS="LITERAL" >C</TT > locale. <TT CLASS="COMMAND" >CREATE DATABASE</TT > will allow superusers to specify <TT CLASS="LITERAL" >SQL_ASCII</TT > encoding regardless of the locale setting, but this choice is deprecated and may result in misbehavior of character-string functions if data that is not encoding-compatible with the locale is stored in the database. </P ><P > The <TT CLASS="LITERAL" >CONNECTION LIMIT</TT > option is only enforced approximately; if two new sessions start at about the same time when just one connection <SPAN CLASS="QUOTE" >"slot"</SPAN > remains for the database, it is possible that both will fail. Also, the limit is not enforced against superusers. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN52067" ></A ><H2 >Examples</H2 ><P > To create a new database: </P><PRE CLASS="PROGRAMLISTING" >CREATE DATABASE lusiadas;</PRE ><P> </P ><P > To create a database <TT CLASS="LITERAL" >sales</TT > owned by user <TT CLASS="LITERAL" >salesapp</TT > with a default tablespace of <TT CLASS="LITERAL" >salesspace</TT >: </P><PRE CLASS="PROGRAMLISTING" >CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;</PRE ><P> </P ><P > To create a database <TT CLASS="LITERAL" >music</TT > which supports the ISO-8859-1 character set: </P><PRE CLASS="PROGRAMLISTING" >CREATE DATABASE music ENCODING 'LATIN1';</PRE ><P> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN52079" ></A ><H2 >Compatibility</H2 ><P > There is no <TT CLASS="COMMAND" >CREATE DATABASE</TT > statement in the SQL standard. Databases are equivalent to catalogs, whose creation is implementation-defined. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN52083" ></A ><H2 >See Also</H2 ><A HREF="sql-alterdatabase.html" ><I >ALTER DATABASE</I ></A >, <A HREF="sql-dropdatabase.html" ><I >DROP DATABASE</I ></A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-createconversion.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="sql-createdomain.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >CREATE CONVERSION</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >CREATE DOMAIN</TD ></TR ></TABLE ></DIV ></BODY ></HTML >