Disabling the psql Pager for the postgres User

By default, psql pipes query output through a pager (less by default) whenever the output is longer than the terminal height. That is convenient in an interactive session, but it gets in the way in two situations I kept running into: running SQL from scripts where you want the full output printed directly, and using psql as the postgres system user (for example in cron jobs or Ansible tasks), where an interactive pager makes no sense at all.

The Problem

The postgres user's home directory is /var/lib/postgresql, and any psql session started with sudo -i -u postgres reads its startup file from there. There is no .psqlrc for that user by default, so every query result is pushed through the pager, and long results leave the session stuck inside less.

The Fix: a .psqlrc File

sudo -i -u postgres
printf '\\pset pager off\n' > /var/lib/postgresql/.psqlrc

Then verify the setting is picked up:

psql
psql (16.x ...)
Type "help" for help.

postgres=# \pset
pager                      : off

You can also confirm it end-to-end by running a query with more output than the terminal height — results should scroll straight through without opening less.

Why This Works

psql reads ~/.psqlrc at startup for interactive sessions (unless you start it with psql -X). Placing \pset pager off in that file makes the setting apply to every interactive session for that user, with no per-command typing.

Alternatives and Gotchas

Related Reading

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles