summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/manage-ag-templatedbs.html
diff options
context:
space:
mode:
Diffstat (limited to 'doc/src/sgml/html/manage-ag-templatedbs.html')
-rw-r--r--doc/src/sgml/html/manage-ag-templatedbs.html88
1 files changed, 88 insertions, 0 deletions
diff --git a/doc/src/sgml/html/manage-ag-templatedbs.html b/doc/src/sgml/html/manage-ag-templatedbs.html
new file mode 100644
index 0000000..0b603c3
--- /dev/null
+++ b/doc/src/sgml/html/manage-ag-templatedbs.html
@@ -0,0 +1,88 @@
+<?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>22.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 V1.79.1" /><link rel="prev" href="manage-ag-createdb.html" title="22.2. Creating a Database" /><link rel="next" href="manage-ag-config.html" title="22.4. Database Configuration" /></head><body id="docContent" class="container-fluid col-10"><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">22.3. Template Databases</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="manage-ag-createdb.html" title="22.2. Creating a Database">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="managing-databases.html" title="Chapter 22. Managing Databases">Up</a></td><th width="60%" align="center">Chapter 22. Managing Databases</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 13.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="manage-ag-config.html" title="22.4. Database Configuration">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="MANAGE-AG-TEMPLATEDBS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">22.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.9.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>
+ There is a second standard system database named
+ <code class="literal">template0</code>.<a id="id-1.6.9.6.3.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.9.6.7.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 xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navfooter"><hr></hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="manage-ag-createdb.html" title="22.2. Creating a Database">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="managing-databases.html" title="Chapter 22. Managing Databases">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="manage-ag-config.html" title="22.4. Database Configuration">Next</a></td></tr><tr><td width="40%" align="left" valign="top">22.2. Creating a Database </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 13.4 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 22.4. Database Configuration</td></tr></table></div></body></html> \ No newline at end of file