Home Explore Blog CI



postgresql

19th chunk of `doc/src/sgml/maintenance.sgml`
cbe90baefb84164ce456722a6fd6c54214a2d3e9fd2e42660000000100000b34
 files so that
   new log files are started and old ones removed after a reasonable
   period of time.
  </para>

  <para>
   If you simply direct the <systemitem>stderr</systemitem> of
   <command>postgres</command> into a
   file, you will have log output, but
   the only way to truncate the log file is to stop and restart
   the server. This might be acceptable if you are using
   <productname>PostgreSQL</productname> in a development environment,
   but few production servers would find this behavior acceptable.
  </para>

  <para>
   A better approach is to send the server's
   <systemitem>stderr</systemitem> output to some type of log rotation program.
   There is a built-in log rotation facility, which you can use by
   setting the configuration parameter <varname>logging_collector</varname> to
   <literal>true</literal> in <filename>postgresql.conf</filename>.  The control
   parameters for this program are described in <xref
   linkend="runtime-config-logging-where"/>. You can also use this approach
   to capture the log data in machine readable <acronym>CSV</acronym>
   (comma-separated values) format.
  </para>

  <para>
   Alternatively, you might prefer to use an external log rotation
   program if you have one that you are already using with other
   server software. For example, the <application>rotatelogs</application>
   tool included in the <productname>Apache</productname> distribution
   can be used with <productname>PostgreSQL</productname>.  One way to
   do this is to pipe the server's
   <systemitem>stderr</systemitem> output to the desired program.
   If you start the server with
   <command>pg_ctl</command>, then <systemitem>stderr</systemitem>
   is already redirected to <systemitem>stdout</systemitem>, so you just need a
   pipe command, for example:

<programlisting>
pg_ctl start | rotatelogs /var/log/pgsql_log 86400
</programlisting>
  </para>

  <para>
   You can combine these approaches by setting up <application>logrotate</application>
   to collect log files produced by <productname>PostgreSQL</productname> built-in
   logging collector.  In this case, the logging collector defines the names and
   location of the log files, while <application>logrotate</application>
   periodically archives these files.  When initiating log rotation,
   <application>logrotate</application> must ensure that the application
   sends further output to the new file.  This is commonly done with a
   <literal>postrotate</literal> script that sends a <literal>SIGHUP</literal>
   signal to the application, which then reopens the log file.
   In <productname>PostgreSQL</productname>, you can run <command>pg_ctl</command>
   with the <literal>logrotate</literal> option instead.  When the server receives
   this command, the server either switches to a new log file or reopens the
   existing file, depending on

Title: Log File Rotation
Summary
This section discusses the importance of log file rotation in PostgreSQL, as log files can become large and unwieldy if not managed properly. It describes various methods for rotating log files, including using the built-in logging collector, external log rotation programs like logrotate or rotatelogs, and piping the server's stderr output to a desired program. The section also explains how to combine these approaches and initiate log rotation using a postrotate script or the pg_ctl command with the logrotate option.