Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/manage-ag.sgml`
fb0a52252d19a1b48b910d3d4040cbadebef53b4014994820000000100000fb4
 <xref linkend="wal"/>)
   has on backup and recovery options. While individual databases in the cluster
   are isolated when considered from the user's perspective, they are closely bound
   from the database administrator's point-of-view.
  </para>

  <para>
   Databases are created with the <command>CREATE DATABASE</command> command
   (see <xref linkend="manage-ag-createdb"/>) and destroyed with the
   <command>DROP DATABASE</command> command
   (see <xref linkend="manage-ag-dropdb"/>).
   To determine the set of existing databases, examine the
   <structname>pg_database</structname> system catalog, for example
<synopsis>
SELECT datname FROM pg_database;
</synopsis>
   The <xref linkend="app-psql"/> program's <literal>\l</literal> meta-command
   and <option>-l</option> command-line option are also useful for listing the
   existing databases.
  </para>

  <note>
   <para>
    The <acronym>SQL</acronym> standard calls databases <quote>catalogs</quote>, but there
    is no difference in practice.
   </para>
  </note>
 </sect1>

 <sect1 id="manage-ag-createdb">
  <title>Creating a Database</title>

  <indexterm><primary>CREATE DATABASE</primary></indexterm>

  <para>
   In order to create a database, the <productname>PostgreSQL</productname>
   server must be up and running (see <xref
   linkend="server-start"/>).
  </para>

  <para>
   Databases are created with the SQL command
   <xref linkend="sql-createdatabase"/>:
<synopsis>
CREATE DATABASE <replaceable>name</replaceable>;
</synopsis>
   where <replaceable>name</replaceable> follows the usual rules for
   <acronym>SQL</acronym> identifiers.  The current role automatically
   becomes the owner of the new database. It is the privilege of the
   owner of a database to remove it later (which also removes all
   the objects in it, even if they have a different owner).
  </para>

  <para>
   The creation of databases is a restricted operation. See <xref
   linkend="role-attributes"/> for how to grant permission.
  </para>

  <para>
   Since you need to be connected to the database server in order to
   execute the <command>CREATE DATABASE</command> command, the
   question remains how the <emphasis>first</emphasis> database at any given
   site can be created. The first database is always created by the
   <command>initdb</command> command when the data storage area is
   initialized. (See <xref linkend="creating-cluster"/>.)  This
   database is called
   <literal>postgres</literal>.<indexterm><primary>postgres</primary></indexterm> So to
   create the first <quote>ordinary</quote> database you can connect to
   <literal>postgres</literal>.
  </para>

  <para>
   Two additional databases,
   <literal>template1</literal><indexterm><primary>template1</primary></indexterm>
   and
   <literal>template0</literal>,<indexterm><primary>template0</primary></indexterm>
   are also created during database cluster initialization.  Whenever a
   new database is created within the
   cluster, <literal>template1</literal> is essentially cloned.
   This means that any changes you make in <literal>template1</literal> are
   propagated to all subsequently created databases. Because of this,
   avoid creating objects in <literal>template1</literal> unless you want them
   propagated to every newly created database.
   <literal>template0</literal> is meant as a pristine copy of the original
   contents of <literal>template1</literal>.  It can be cloned instead
   of <literal>template1</literal> when it is important to make a database
   without any such site-local additions.  More details
   appear in <xref linkend="manage-ag-templatedbs"/>.
  </para>

  <para>
   As a convenience, there is a program you can
   execute from the shell to create new databases,
   <command>createdb</command>.<indexterm><primary>createdb</primary></indexterm>

<synopsis>
createdb <replaceable class="parameter">dbname</replaceable>
</synopsis>

   <command>createdb</command> does no magic. It connects to the <literal>postgres</literal>

Title: Creating a Database
Summary
Databases are created using the CREATE DATABASE command, requiring the PostgreSQL server to be running. The current role becomes the owner of the new database, with the privilege to remove it. Database creation is a restricted operation, requiring appropriate permissions. The first database is created during data storage initialization using initdb, and is named 'postgres'. Two additional databases, 'template1' and 'template0', are also created, with 'template1' serving as a template for new databases and 'template0' as a pristine copy. The createdb program provides a convenient shell command for creating new databases.