How to add database name to log output

PostgreSQL allows you to customize statement logging in numerous ways. You can globally set the level of logging you want at both the postgresql.conf (that will affect all databases) or at the database level using the various log_statement* variables. Most of these are documented in runtime config logging.

For this brief article, we'll talk about the log_line_prefix variable. By default this variable is very minimalistic and just prefixes the log lines with the date time of the statement. If you have just one database (like the way Oracle runs) this is not a big deal, but if you are like us and run several databases on one PostgreSQL instance, it would be nice to as part of the prefix include the database name.

Customizing Log Line Prefix

You can do this by changing your log_line_prefix in your postgresql.conf to include the %d variable. Something like
log_line_prefix = '%t %d '

You can also edit by using pgAdmin Server Configuration option.

After you have done this, make sure to reload the config with:

SELECT pg_reload_conf();

Or clicking the reload (green arrow icon) in pgAdmin Server Config dialog

The log_line_prefix can also include various other useful variables if you choose such as the %u user, and %h remote host.