Where Does psql Store Command History?
psql command history file and the variables that move it, plus the startup rc file.
Last updated
psql logs every command to a history file in your home directory, with readline providing the search and recall. The default path is fixed, but two environment hooks change the story: PSQL_HISTORY relocates the file and PSQL_HISTSIZE caps its length.
History is per user, not per database, so one file accumulates commands across every server you touch. That blending surprises people auditing queries later. A separate startup file, .psqlrc, handles defaults like formatting and timing for all sessions at once.
Where psql stores this, by platform
~/.psql_history
Readline format, shared across databases. Set PSQL_HISTORY in your shell rc to relocate it permanently. Startup defaults live separately in ~/.psqlrc beside it.
~/.psql_history
Same file name and variables as Linux. Homebrew and Postgres.app installs share this location, so the history follows you across install methods on the same machine.
Frequently asked questions
how do i move the psql history file
Set PSQL_HISTORY to the path you want before launching psql. The file follows the variable every session, which suits synced dotfiles or per project histories. Unset it to fall back to the default.
can psql run settings at startup
Yes, in ~/.psqlrc. Meta commands like \x auto or \timing on run at every startup, and the file applies to all databases you connect to. Keep it in version control alongside the history path decision.
Notice an outdated path? Let us know.