PostgreSQL – View Operation Timestamps for Long Running Operations in PSQL

postgresqlpsql

When I do long running queries, I tend to use tmux and a psql console. It will not show me any output of when some operation was started or completed.
How can I get the psql console to show some timestamps?

Clarification:
I'm hoping for a non-scripting solution, just in the psql console. (Or does nobody use that console?) I'm maybe naive but still trying to do most of the database stuff using the database itself and its tools (using procedures, etc.)

Best Answer

There are various options for retrieving timing information while using psql.

  1. The \timing command.
  2. In scripts you can use
\echo `date`

to insert the date in the output.

  1. You can have the date in prompt strings using eg
\set PROMPT1 = '%`date` %n@%m:%/%R%# '

For both 2 and 3 you can supply arguments to date to get the date in the format you want.

  1. If using psql non-interactively, you can pipe the output through ts (from moreutils) or through awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'(GNU's version of awk is needed) to prepend a timestamp to each line of output.
Related Question