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 operating-system-specific suggestions. (In each
case be sure to use the proper installation directory and user
name where we show generic values.)
<itemizedlist>
<listitem>
<para>
For <productname>FreeBSD</productname>, look at the file
<filename>contrib/start-scripts/freebsd</filename> in the
<productname>PostgreSQL</productname> source distribution.
<indexterm><primary>FreeBSD</primary><secondary>start script</secondary></indexterm>
</para>
</listitem>
<listitem>
<para>
On <productname>OpenBSD</productname>, add the following lines
to the file <filename>/etc/rc.local</filename>:
<indexterm><primary>OpenBSD</primary><secondary>start script</secondary></indexterm>
<programlisting>
if [ -x /usr/local/pgsql/bin/pg_ctl -a -x /usr/local/pgsql/bin/postgres ]; then
su -l postgres -c '/usr/local/pgsql/bin/pg_ctl start -s -l /var/postgresql/log -D /usr/local/pgsql/data'
echo -n ' postgresql'
fi
</programlisting>
</para>
</listitem>
<listitem>
<para>
On <productname>Linux</productname> systems either add
<indexterm><primary>Linux</primary><secondary>start script</secondary></indexterm>
<programlisting>
/usr/local/pgsql/bin/pg_ctl start -l logfile -D /usr/local/pgsql/data
</programlisting>
to <filename>/etc/rc.d/rc.local</filename>
or <filename>/etc/rc.local</filename> or look at the file
<filename>contrib/start-scripts/linux</filename> in the
<productname>PostgreSQL</productname> source distribution.
</para>
<para>
When using <application>systemd</application>, you can use the following
service unit file (e.g.,
at <filename>/etc/systemd/system/postgresql.service</filename>):<indexterm><primary>systemd</primary></indexterm>
<programlisting>
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=infinity
[Install]
WantedBy=multi-user.target
</programlisting>
Using <literal>Type=notify</literal> requires that the server binary was
built with <literal>configure --with-systemd</literal>.
</para>
<para>
Consider carefully the timeout
setting. <application>systemd</application> has a default timeout of 90
seconds as of this writing and will kill a process that does not report
readiness within that time. But a <productname>PostgreSQL</productname>
server that might have to perform crash recovery at startup could take
much longer to become ready. The suggested value
of <literal>infinity</literal> disables the timeout logic.
</para>
</listitem>
<listitem>
<para>
On <productname>NetBSD</productname>, use either the
<productname>FreeBSD</productname>