are two more databases created within
each cluster during initialization, named <literal>template1</literal>
and <literal>template0</literal>. As the names suggest, these will be
used as templates for subsequently-created databases; they should not be
used for actual work. (See <xref linkend="managing-databases"/> for
information about creating new databases within a cluster.)
</para>
<para>
In file system terms, a database cluster is a single directory
under which all data will be stored. We call this the <firstterm>data
directory</firstterm> or <firstterm>data area</firstterm>. It is
completely up to you where you choose to store your data. There is no
default, although locations such as
<filename>/usr/local/pgsql/data</filename> or
<filename>/var/lib/pgsql/data</filename> are popular.
The data directory must be initialized before being used, using the program
<xref linkend="app-initdb"/><indexterm><primary>initdb</primary></indexterm>
which is installed with <productname>PostgreSQL</productname>.
</para>
<para>
If you are using a pre-packaged version
of <productname>PostgreSQL</productname>, it may well have a specific
convention for where to place the data directory, and it may also
provide a script for creating the data directory. In that case you
should use that script in preference to
running <command>initdb</command> directly.
Consult the package-level documentation for details.
</para>
<para>
To initialize a database cluster manually,
run <command>initdb</command> and specify the desired
file system location of the database cluster with the
<option>-D</option> option, for example:
<screen>
<prompt>$</prompt> <userinput>initdb -D /usr/local/pgsql/data</userinput>
</screen>
Note that you must execute this command while logged into the
<productname>PostgreSQL</productname> user account, which is
described in the previous section.
</para>
<tip>
<para>
As an alternative to the <option>-D</option> option, you can set
the environment variable <envar>PGDATA</envar>.
<indexterm><primary><envar>PGDATA</envar></primary></indexterm>
</para>
</tip>
<para>
Alternatively, you can run <command>initdb</command> via
the <xref linkend="app-pg-ctl"/>
program<indexterm><primary>pg_ctl</primary></indexterm> like so:
<screen>
<prompt>$</prompt> <userinput>pg_ctl -D /usr/local/pgsql/data initdb</userinput>
</screen>
This may be more intuitive if you are
using <command>pg_ctl</command> for starting and stopping the
server (see <xref linkend="server-start"/>), so
that <command>pg_ctl</command> would be the sole command you use
for managing the database server instance.
</para>
<para>
<command>initdb</command> will attempt to create the directory you
specify if it does not already exist. Of course, this will fail if
<command>initdb</command> does not have permissions to write in the
parent directory. It's generally recommendable that the
<productname>PostgreSQL</productname> user own not just the data
directory but its parent directory as well, so that this should not
be a problem. If the desired parent directory doesn't exist either,
you will need to create it first, using root privileges if the
grandparent directory isn't writable. So the process might look
like this:
<screen>
root# <userinput>mkdir /usr/local/pgsql</userinput>
root# <userinput>chown postgres /usr/local/pgsql</userinput>
root# <userinput>su postgres</userinput>
postgres$ <userinput>initdb -D /usr/local/pgsql/data</userinput>
</screen>
</para>
<para>
<command>initdb</command> will refuse to run if the data directory
exists and already contains files; this is to prevent accidentally
overwriting an existing installation.
</para>
<para>
Because the data directory contains all the data stored in the
database, it is essential that it be secured from unauthorized