summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/manage-ag-templatedbs.html
blob: 6331df000992f50f0d67033798a19205dbaa9214 (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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>23.3. Template Databases</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="manage-ag-createdb.html" title="23.2. Creating a Database" /><link rel="next" href="manage-ag-config.html" title="23.4. Database Configuration" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">23.3. Template Databases</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="manage-ag-createdb.html" title="23.2. Creating a Database">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="managing-databases.html" title="Chapter 23. Managing Databases">Up</a></td><th width="60%" align="center">Chapter 23. Managing Databases</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.6 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="manage-ag-config.html" title="23.4. Database Configuration">Next</a></td></tr></table><hr /></div><div class="sect1" id="MANAGE-AG-TEMPLATEDBS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">23.3. Template Databases</h2></div></div></div><p>
   <code class="command">CREATE DATABASE</code> actually works by copying an existing
   database.  By default, it copies the standard system database named
   <code class="literal">template1</code>.<a id="id-1.6.10.6.2.3" class="indexterm"></a> Thus that
   database is the <span class="quote"><span class="quote">template</span></span> from which new databases are
   made.  If you add objects to <code class="literal">template1</code>, these objects
   will be copied into subsequently created user databases.  This
   behavior allows site-local modifications to the standard set of
   objects in databases.  For example, if you install the procedural
   language <span class="application">PL/Perl</span> in <code class="literal">template1</code>, it will
   automatically be available in user databases without any extra
   action being taken when those databases are created.
  </p><p>
   However, <code class="command">CREATE DATABASE</code> does not copy database-level
   <code class="command">GRANT</code> permissions attached to the source database.
   The new database has default database-level permissions.
  </p><p>
   There is a second standard system database named
   <code class="literal">template0</code>.<a id="id-1.6.10.6.4.2" class="indexterm"></a> This
   database contains the same data as the initial contents of
   <code class="literal">template1</code>, that is, only the standard objects
   predefined by your version of
   <span class="productname">PostgreSQL</span>.  <code class="literal">template0</code>
   should never be changed after the database cluster has been
   initialized.  By instructing
   <code class="command">CREATE DATABASE</code> to copy <code class="literal">template0</code> instead
   of <code class="literal">template1</code>, you can create a <span class="quote"><span class="quote">pristine</span></span> user
   database (one where no user-defined objects exist and where the system
   objects have not been altered) that contains none of the site-local additions in
   <code class="literal">template1</code>.  This is particularly handy when restoring a
   <code class="literal">pg_dump</code> dump: the dump script should be restored in a
   pristine database to ensure that one recreates the correct contents
   of the dumped database, without conflicting with objects that
   might have been added to <code class="literal">template1</code> later on.
  </p><p>
   Another common reason for copying <code class="literal">template0</code> instead
   of <code class="literal">template1</code> is that new encoding and locale settings
   can be specified when copying <code class="literal">template0</code>, whereas a copy
   of <code class="literal">template1</code> must use the same settings it does.
   This is because <code class="literal">template1</code> might contain encoding-specific
   or locale-specific data, while <code class="literal">template0</code> is known not to.
  </p><p>
   To create a database by copying <code class="literal">template0</code>, use:
</p><pre class="programlisting">
CREATE DATABASE <em class="replaceable"><code>dbname</code></em> TEMPLATE template0;
</pre><p>
   from the SQL environment, or:
</p><pre class="programlisting">
createdb -T template0 <em class="replaceable"><code>dbname</code></em>
</pre><p>
   from the shell.
  </p><p>
   It is possible to create additional template databases, and indeed
   one can copy any database in a cluster by specifying its name
   as the template for <code class="command">CREATE DATABASE</code>.  It is important to
   understand, however, that this is not (yet) intended as
   a general-purpose <span class="quote"><span class="quote"><code class="command">COPY DATABASE</code></span></span> facility.
   The principal limitation is that no other sessions can be connected to
   the source database while it is being copied.  <code class="command">CREATE
   DATABASE</code> will fail if any other connection exists when it starts;
   during the copy operation, new connections to the source database
   are prevented.
  </p><p>
   Two useful flags exist in <code class="literal">pg_database</code><a id="id-1.6.10.6.8.2" class="indexterm"></a> for each
   database: the columns <code class="literal">datistemplate</code> and
   <code class="literal">datallowconn</code>.  <code class="literal">datistemplate</code>
   can be set to indicate that a database is intended as a template for
   <code class="command">CREATE DATABASE</code>.  If this flag is set, the database can be
   cloned by any user with <code class="literal">CREATEDB</code> privileges; if it is not set,
   only superusers and the owner of the database can clone it.
   If <code class="literal">datallowconn</code> is false, then no new connections
   to that database will be allowed (but existing sessions are not terminated
   simply by setting the flag false).  The <code class="literal">template0</code>
   database is normally marked <code class="literal">datallowconn = false</code> to prevent its modification.
   Both <code class="literal">template0</code> and <code class="literal">template1</code>
   should always be marked with <code class="literal">datistemplate = true</code>.
  </p><div class="note"><h3 class="title">Note</h3><p>
    <code class="literal">template1</code> and <code class="literal">template0</code> do not have any special
    status beyond the fact that the name <code class="literal">template1</code> is the default
    source database name for <code class="command">CREATE DATABASE</code>.
    For example, one could drop <code class="literal">template1</code> and recreate it from
    <code class="literal">template0</code> without any ill effects.  This course of action
    might be advisable if one has carelessly added a bunch of junk in
    <code class="literal">template1</code>. (To delete <code class="literal">template1</code>,
    it must have <code class="literal">pg_database.datistemplate = false</code>.)
   </p><p>
    The <code class="literal">postgres</code> database is also created when a database
    cluster is initialized.  This database is meant as a default database for
    users and applications to connect to. It is simply a copy of
    <code class="literal">template1</code> and can be dropped and recreated if necessary.
   </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="manage-ag-createdb.html" title="23.2. Creating a Database">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="managing-databases.html" title="Chapter 23. Managing Databases">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="manage-ag-config.html" title="23.4. Database Configuration">Next</a></td></tr><tr><td width="40%" align="left" valign="top">23.2. Creating a Database </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.6 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 23.4. Database Configuration</td></tr></table></div></body></html>