Today I Learned

A Hashrocket project

Watch a Query with psql

Postgres’ REPL psql includes a \watch meta command that repeatedly executes a query every n seconds. Here’s the official description:

\watch [ seconds ] Repeatedly execute the current query buffer (like \g) until interrupted or the query fails. Wait the specified number of seconds (default 2) between executions.

\watch executes the current query buffer. If you aren’t sure what that query is, print it with \p:

dev=# \p
select title from posts;

Run \watch to ‘tail’ that query result as it changes:

dev=# \watch
Sun Jul 29 17:06:13 2018 (every 2s)

        title
----------------------
 Psql Watch
(1 row)

It prints a timestamp each time it watches.

psql docs

Looking for help? Hashrocket developers believe that data quality is as important as code quality. We enjoy all the challenges of relational databases, from finding the fastest index, to structuring data to fit the needs of an application. We're eager to share our experiences; check out PG Casts, a series of free weekly PostgreSQL screencasts.