summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/ref/create_database.sgml
blob: 420576c5e83eb9542cccd961f2a218c80566e09d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!--
doc/src/sgml/ref/create_database.sgml
PostgreSQL documentation
-->

<refentry id="sql-createdatabase">
 <indexterm zone="sql-createdatabase">
  <primary>CREATE DATABASE</primary>
 </indexterm>

 <refmeta>
  <refentrytitle>CREATE DATABASE</refentrytitle>
  <manvolnum>7</manvolnum>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>

 <refnamediv>
  <refname>CREATE DATABASE</refname>
  <refpurpose>create a new database</refpurpose>
 </refnamediv>

 <refsynopsisdiv>
<synopsis>
CREATE DATABASE <replaceable class="parameter">name</replaceable>
    [ [ WITH ] [ OWNER [=] <replaceable class="parameter">user_name</replaceable> ]
           [ TEMPLATE [=] <replaceable class="parameter">template</replaceable> ]
           [ ENCODING [=] <replaceable class="parameter">encoding</replaceable> ]
           [ LOCALE [=] <replaceable class="parameter">locale</replaceable> ]
           [ LC_COLLATE [=] <replaceable class="parameter">lc_collate</replaceable> ]
           [ LC_CTYPE [=] <replaceable class="parameter">lc_ctype</replaceable> ]
           [ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
           [ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
           [ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
           [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
</synopsis>
 </refsynopsisdiv>

 <refsect1>
  <title>Description</title>

  <para>
   <command>CREATE DATABASE</command> creates a new
   <productname>PostgreSQL</productname> database.
  </para>

  <para>
   To create a database, you must be a superuser or have the special
   <literal>CREATEDB</literal> privilege.
   See <xref linkend="sql-createrole"/>.
  </para>

  <para>
   By default, the new database will be created by cloning the standard
   system database <literal>template1</literal>.  A different template can be
   specified by writing <literal>TEMPLATE
   <replaceable class="parameter">name</replaceable></literal>.  In particular,
   by writing <literal>TEMPLATE template0</literal>, you can create a pristine
   database (one where no user-defined objects exist and where the system
   objects have not been altered)
   containing only the standard objects predefined by your
   version of <productname>PostgreSQL</productname>.  This is useful
   if you wish to avoid copying
   any installation-local objects that might have been added to
   <literal>template1</literal>.
  </para>
 </refsect1>

 <refsect1>
  <title>Parameters</title>

    <variablelist>
     <varlistentry>
      <term><replaceable class="parameter">name</replaceable></term>
      <listitem>
       <para>
        The name of a database to create.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">user_name</replaceable></term>
      <listitem>
       <para>
        The role name of the user who will own the new database,
        or <literal>DEFAULT</literal> to use the default (namely, the
        user executing the command).  To create a database owned by another
        role, you must be a direct or indirect member of that role,
        or be a superuser.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">template</replaceable></term>
      <listitem>
       <para>
        The name of the template from which to create the new database,
        or <literal>DEFAULT</literal> to use the default template
        (<literal>template1</literal>).
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">encoding</replaceable></term>
      <listitem>
       <para>
        Character set encoding to use in the new database.  Specify
        a string constant (e.g., <literal>'SQL_ASCII'</literal>),
        or an integer encoding number, or <literal>DEFAULT</literal>
        to use the default encoding (namely, the encoding of the
        template database). The character sets supported by the
        <productname>PostgreSQL</productname> server are described in
        <xref linkend="multibyte-charset-supported"/>. See below for
        additional restrictions.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">locale</replaceable></term>
      <listitem>
       <para>
        This is a shortcut for setting <symbol>LC_COLLATE</symbol>
        and <symbol>LC_CTYPE</symbol> at once.  If you specify this,
        you cannot specify either of those parameters.
       </para>
       <tip>
        <para>
         The other locale settings <xref linkend="guc-lc-messages"/>, <xref
         linkend="guc-lc-monetary"/>, <xref linkend="guc-lc-numeric"/>, and
         <xref linkend="guc-lc-time"/> are not fixed per database and are not
         set by this command.  If you want to make them the default for a
         specific database, you can use <literal>ALTER DATABASE
         ... SET</literal>.
        </para>
       </tip>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">lc_collate</replaceable></term>
      <listitem>
       <para>
        Collation order (<literal>LC_COLLATE</literal>) to use in the new database.
        This affects the sort order applied to strings, e.g., in queries with
        ORDER BY, as well as the order used in indexes on text columns.
        The default is to use the collation order of the template database.
        See below for additional restrictions.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">lc_ctype</replaceable></term>
      <listitem>
       <para>
        Character classification (<literal>LC_CTYPE</literal>) to use in the new
        database. This affects the categorization of characters, e.g., lower,
        upper and digit. The default is to use the character classification of
        the template database. See below for additional restrictions.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">tablespace_name</replaceable></term>
      <listitem>
       <para>
        The name of the tablespace that will be associated with the
        new database, or <literal>DEFAULT</literal> to use the
        template database's tablespace. This
        tablespace will be the default tablespace used for objects
        created in this database. See
        <xref linkend="sql-createtablespace"/>
        for more information.
       </para>
      </listitem>
     </varlistentry>

      <varlistentry>
       <term><replaceable class="parameter">allowconn</replaceable></term>
       <listitem>
        <para>
         If false then no one can connect to this database.  The default is
         true, allowing connections (except as restricted by other mechanisms,
         such as <literal>GRANT</literal>/<literal>REVOKE CONNECT</literal>).
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
      <term><replaceable class="parameter">connlimit</replaceable></term>
      <listitem>
       <para>
        How many concurrent connections can be made
        to this database.  -1 (the default) means no limit.
       </para>
      </listitem>
     </varlistentry>

      <varlistentry>
       <term><replaceable class="parameter">istemplate</replaceable></term>
       <listitem>
        <para>
         If true, then this database can be cloned by any user with <literal>CREATEDB</literal>
         privileges; if false (the default), then only superusers or the owner
         of the database can clone it.
        </para>
       </listitem>
      </varlistentry>
    </variablelist>

  <para>
   Optional parameters can be written in any order, not only the order
   illustrated above.
  </para>
 </refsect1>

 <refsect1>
  <title>Notes</title>

   <para>
    <command>CREATE DATABASE</command> cannot be executed inside a transaction
    block.
   </para>

   <para>
    Errors along the line of <quote>could not initialize database directory</quote>
    are most likely related to insufficient permissions on the data
    directory, a full disk, or other file system problems.
   </para>

   <para>
    Use <xref linkend="sql-dropdatabase"/> to remove a database.
   </para>

   <para>
    The program <xref linkend="app-createdb"/> is a
    wrapper program around this command, provided for convenience.
   </para>

   <para>
    Database-level configuration parameters (set via <xref
    linkend="sql-alterdatabase"/>) and database-level permissions (set via
    <xref linkend="sql-grant"/>) are not copied from the template database.
   </para>

  <para>
   Although it is possible to copy a database other than <literal>template1</literal>
   by specifying its name as the template, this is not (yet) intended as
   a general-purpose <quote><command>COPY DATABASE</command></quote> facility.
   The principal limitation is that no other sessions can be connected to
   the template database while it is being copied.  <command>CREATE
   DATABASE</command> will fail if any other connection exists when it starts;
   otherwise, new connections to the template database are locked out
   until <command>CREATE DATABASE</command> completes.
   See <xref linkend="manage-ag-templatedbs"/> for more information.
  </para>

  <para>
   The character set encoding specified for the new database must be
   compatible with the chosen locale settings (<literal>LC_COLLATE</literal> and
   <literal>LC_CTYPE</literal>).  If the locale is <literal>C</literal> (or equivalently
   <literal>POSIX</literal>), then all encodings are allowed, but for other
   locale settings there is only one encoding that will work properly.
   (On Windows, however, UTF-8 encoding can be used with any locale.)
   <command>CREATE DATABASE</command> will allow superusers to specify
   <literal>SQL_ASCII</literal> encoding regardless of the locale settings,
   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.
  </para>

  <para>
   The encoding and locale settings must match those of the template database,
   except when <literal>template0</literal> is used as template.  This is because
   other databases might contain data that does not match the specified
   encoding, or might contain indexes whose sort ordering is affected by
   <literal>LC_COLLATE</literal> and <literal>LC_CTYPE</literal>.  Copying such data would
   result in a database that is corrupt according to the new settings.
   <literal>template0</literal>, however, is known to not contain any data or
   indexes that would be affected.
  </para>

  <para>
   The <literal>CONNECTION LIMIT</literal> option is only enforced approximately;
   if two new sessions start at about the same time when just one
   connection <quote>slot</quote> remains for the database, it is possible that
   both will fail.  Also, the limit is not enforced against superusers or
   background worker processes.
  </para>
 </refsect1>

 <refsect1>
  <title>Examples</title>

  <para>
   To create a new database:

<programlisting>
CREATE DATABASE lusiadas;
</programlisting>
  </para>

  <para>
   To create a database <literal>sales</literal> owned by user <literal>salesapp</literal>
   with a default tablespace of <literal>salesspace</literal>:

<programlisting>
CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
</programlisting>
  </para>

  <para>
   To create a database <literal>music</literal> with a different locale:
<programlisting>
CREATE DATABASE music
    LOCALE 'sv_SE.utf8'
    TEMPLATE template0;
</programlisting>
    In this example, the <literal>TEMPLATE template0</literal> clause is required if
    the specified locale is different from the one in <literal>template1</literal>.
    (If it is not, then specifying the locale explicitly is redundant.)
  </para>

  <para>
   To create a database <literal>music2</literal> with a different locale and a
   different character set encoding:
<programlisting>
CREATE DATABASE music2
    LOCALE 'sv_SE.iso885915'
    ENCODING LATIN9
    TEMPLATE template0;
</programlisting>
   The specified locale and encoding settings must match, or an error will be
   reported.
  </para>

  <para>
   Note that locale names are specific to the operating system, so that the
   above commands might not work in the same way everywhere.
  </para>
 </refsect1>

 <refsect1>
  <title>Compatibility</title>

  <para>
   There is no <command>CREATE DATABASE</command> statement in the SQL
   standard.  Databases are equivalent to catalogs, whose creation is
   implementation-defined.
  </para>
 </refsect1>

 <refsect1>
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="sql-alterdatabase"/></member>
   <member><xref linkend="sql-dropdatabase"/></member>
  </simplelist>
 </refsect1>

</refentry>