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
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>16.2.Creating a Database Cluster</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="runtime.html" title="Chapter16.Operating System Environment">
<link rel="prev" href="runtime.html" title="Chapter16.Operating System Environment">
<link rel="next" href="postmaster-start.html" title="16.3.Starting the Database Server">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="creating-cluster"></a>16.2.Creating a Database Cluster</h2></div></div></div>
<a name="id640618"></a><a name="id640624"></a><p> Before you can do anything, you must initialize a database storage
area on disk. We call this a <em class="firstterm">database cluster</em>.
(<acronym class="acronym">SQL</acronym> uses the term catalog cluster.) A
database cluster is a collection of databases that is managed by a
single instance of a running database server. After initialization, a
database cluster will contain a database named <code class="literal">postgres</code>,
which is meant as a default database for use by utilities, users and third
party applications. The database server itself does not require the
<code class="literal">postgres</code> database to exist, but many external utility
programs assume it exists. Another database created within each cluster
during initialization is called
<code class="literal">template1</code>. As the name suggests, this will be used
as a template for subsequently created databases; it should not be
used for actual work. (See <a href="managing-databases.html" title="Chapter19.Managing Databases">Chapter19, <i>Managing Databases</i></a> for
information about creating new databases within a cluster.)
</p>
<p> In file system terms, a database cluster will be a single directory
under which all data will be stored. We call this the <em class="firstterm">data
directory</em> or <em class="firstterm">data area</em>. It is
completely up to you where you choose to store your data. There is no
default, although locations such as
<code class="filename">/usr/local/pgsql/data</code> or
<code class="filename">/var/lib/pgsql/data</code> are popular. To initialize a
database cluster, use the command <a href="app-initdb.html" title="initdb"><span class="refentrytitle"><a name="app-initdb-title"></a>initdb</span></a>,<a name="id640723"></a> which is
installed with <span class="productname">PostgreSQL</span>. The desired
file system location of your database cluster is indicated by the
<code class="option">-D</code> option, for example
</p>
<pre class="screen"><code class="prompt">$</code> <strong class="userinput"><code>initdb -D /usr/local/pgsql/data</code></strong></pre>
<p>
Note that you must execute this command while logged into the
<span class="productname">PostgreSQL</span> user account, which is
described in the previous section.
</p>
<div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Tip</h3>
<p> As an alternative to the <code class="option">-D</code> option, you can set
the environment variable <code class="envar">PGDATA</code>.
<a name="id640782"></a>
</p>
</div>
<p> <code class="command">initdb</code> will attempt to create the directory you
specify if it does not already exist. It is likely that it will not
have the permission to do so (if you followed our advice and created
an unprivileged account). In that case you should create the
directory yourself (as root) and change the owner to be the
<span class="productname">PostgreSQL</span> user. Here is how this might
be done:
</p>
<pre class="screen">root# <strong class="userinput"><code>mkdir /usr/local/pgsql/data</code></strong>
root# <strong class="userinput"><code>chown postgres /usr/local/pgsql/data</code></strong>
root# <strong class="userinput"><code>su postgres</code></strong>
postgres$ <strong class="userinput"><code>initdb -D /usr/local/pgsql/data</code></strong></pre>
<p>
</p>
<p> <code class="command">initdb</code> will refuse to run if the data directory
looks like it has already been initialized.</p>
<p> Because the data directory contains all the data stored in the
database, it is essential that it be secured from unauthorized
access. <code class="command">initdb</code> therefore revokes access
permissions from everyone but the
<span class="productname">PostgreSQL</span> user.
</p>
<p> However, while the directory contents are secure, the default
client authentication setup allows any local user to connect to the
database and even become the database superuser. If you do not
trust other local users, we recommend you use one of
<code class="command">initdb</code>'s <code class="option">-W</code>, <code class="option">--pwprompt</code>
or <code class="option">--pwfile</code> options to assign a password to the
database superuser.<a name="id640901"></a> Also, specify <code class="option">-A md5</code> or
<code class="option">-A password</code> so that the default <code class="literal">trust</code> authentication
mode is not used; or modify the generated <code class="filename">pg_hba.conf</code>
file after running <code class="command">initdb</code>,
<span class="emphasis"><em>before</em></span> you start the server for the first time. (Other
reasonable approaches include using <code class="literal">ident</code> authentication
or file system permissions to restrict connections. See <a href="client-authentication.html" title="Chapter20.Client Authentication">Chapter20, <i>Client Authentication</i></a> for more information.)
</p>
<p> <code class="command">initdb</code> also initializes the default
locale<a name="id640974"></a> for the database cluster.
Normally, it will just take the locale settings in the environment
and apply them to the initialized database. It is possible to
specify a different locale for the database; more information about
that can be found in <a href="charset.html#locale" title="21.1.Locale Support">Section21.1, “Locale Support”</a>. The sort order used
within a particular database cluster is set by
<code class="command">initdb</code> and cannot be changed later, short of
dumping all data, rerunning <code class="command">initdb</code>, and reloading
the data. There is also a performance impact for using locales
other than <code class="literal">C</code> or <code class="literal">POSIX</code>. Therefore, it is
important to make this choice correctly the first time.
</p>
<p> <code class="command">initdb</code> also sets the default character set encoding
for the database cluster. Normally this should be chosen to match the
locale setting. For details see <a href="multibyte.html" title="21.2.Character Set Support">Section21.2, “Character Set Support”</a>.
</p>
</div></body>
</html>
|