Postgresql – Logging to a file in PostgreSQL windows service

logpostgresqlwindows

I need an alternative for running the following command:

C:\xxx\pgsql\bin\pg_ctl" -D "C:\xxx\pgsql\data" -l "C:\yyy\log\pgsql.log" start.

This way the server starts and logs to C:\yyy\log\pgsql.log.
When I try to register the server as a service though – no logging options are available and the server logs to Event Viewer.
From pg_ctl documentation:

pg_ctl register [-N servicename] [-U username] [-P password] [-D datadir] [-S a[uto] | d[emand] ] [-w] [-t seconds] [-s] [-o options]

How can I force the server to log into a file?
Note: I don't want to use one-click installer, I just want to work with unzipped binaries.

Best Answer

Locate your postgresql.conf in the datadir

Find the section that looks like this

#------------------------------------------------------------------------------
# ERROR REPORTING AND LOGGING
#------------------------------------------------------------------------------

# - Where to Log -

#log_destination = 'stderr'             # Valid values are combinations of
                                        # stderr, csvlog, syslog and eventlog,
                                        # depending on platform.  csvlog
                                        # requires logging_collector to be on.

# This is used when logging to stderr:
#logging_collector = off                # Enable capturing of stderr and csvlog
                                        # into log files. Required to be on for
                                        # csvlogs.
                                        # (change requires restart)

# These are only used if logging_collector is on:
#log_directory = 'pg_log'               # directory where log files are written,
                                        # can be absolute or relative to PGDATA
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'        # log file name pattern,
                                        # can include strftime() escapes
#log_truncate_on_rotation = off         # If on, an existing log file of the
                                        # same name as the new log file will be
                                        # truncated rather than appended to.
                                        # But such truncation only occurs on
                                        # time-driven rotation, not on restarts
                                        # or size-driven rotation.  Default is
                                        # off, meaning append to existing files
                                        # in all cases.
log_rotation_age = 7d                   # Automatic rotation of logfiles will
                                        # happen after that time.  0 disables.
#log_rotation_size = 10MB               # Automatic rotation of logfiles will
                                        # happen after that much log output.
                                        # 0 disables.

simply uncomment and use these parameters. Then, restart the postgres service,

Since you are running postgresql in Windows, it is possible that you may not be allowed to edit postgresql.conf while the service is up. If that is the case:

  • shutdown the postgres service
  • edit postgresql.conf
  • start the postgres service

Give it a Try !!!