at other levels.
</para>
</sect3>
</sect2>
</sect1>
<sect1 id="server-start">
<title>Starting the Database Server</title>
<para>
Before anyone can access the database, you must start the database
server. The database server program is called
<command>postgres</command>.<indexterm><primary>postgres</primary></indexterm>
</para>
<para>
If you are using a pre-packaged version
of <productname>PostgreSQL</productname>, it almost certainly includes
provisions for running the server as a background task according to the
conventions of your operating system. Using the package's
infrastructure to start the server will be much less work than figuring
out how to do this yourself. Consult the package-level documentation
for details.
</para>
<para>
The bare-bones way to start the server manually is just to invoke
<command>postgres</command> directly, specifying the location of the
data directory with the <option>-D</option> option, for example:
<screen>
$ <userinput>postgres -D /usr/local/pgsql/data</userinput>
</screen>
which will leave the server running in the foreground. This must be
done while logged into the <productname>PostgreSQL</productname> user
account. Without <option>-D</option>, the server will try to use
the data directory named by the environment variable <envar>PGDATA</envar>.
If that variable is not provided either, it will fail.
</para>
<para>
Normally it is better to start <command>postgres</command> in the
background. For this, use the usual Unix shell syntax:
<screen>
$ <userinput>postgres -D /usr/local/pgsql/data >logfile 2>&1 &</userinput>
</screen>
It is important to store the server's <systemitem>stdout</systemitem> and
<systemitem>stderr</systemitem> output somewhere, as shown above. It will help
for auditing purposes and to diagnose problems. (See <xref
linkend="logfile-maintenance"/> for a more thorough discussion of log
file handling.)
</para>
<para>
The <command>postgres</command> program also takes a number of other
command-line options. For more information, see the
<xref linkend="app-postgres"/> reference page
and <xref linkend="runtime-config"/> below.
</para>
<para>
This shell syntax can get tedious quickly. Therefore the wrapper
program
<xref linkend="app-pg-ctl"/><indexterm><primary>pg_ctl</primary></indexterm>
is provided to simplify some tasks. For example:
<programlisting>
pg_ctl start -l logfile
</programlisting>
will start the server in the background and put the output into the
named log file. The <option>-D</option> option has the same meaning
here as for <command>postgres</command>. <command>pg_ctl</command>
is also capable of stopping the server.
</para>
<para>
Normally, you will want to start the database server when the
computer boots.<indexterm>
<primary>booting</primary>
<secondary>starting the server during</secondary>
</indexterm>
Autostart scripts are operating-system-specific.
There are a few example scripts distributed with
<productname>PostgreSQL</productname> in the
<filename>contrib/start-scripts</filename> directory. Installing one will require
root privileges.
</para>
<para>
Different systems have different conventions for starting up daemons
at boot time. Many systems have a file
<filename>/etc/rc.local</filename> or
<filename>/etc/rc.d/rc.local</filename>. Others use <filename>init.d</filename> or
<filename>rc.d</filename> directories. Whatever you do, the server must be
run by the <productname>PostgreSQL</productname> user account
<emphasis>and not by root</emphasis> or any other user. Therefore you
probably should form your commands using
<literal>su postgres -c '...'</literal>. For example:
<programlisting>
su postgres -c 'pg_ctl start -D /usr/local/pgsql/data -l serverlog'
</programlisting>
</para>
<para>
Here are a few more