psqlrc - the psql config file

created onOctober 8, 2025
last modified onOctober 9, 2025

The configuration file for is .

There are several options for the location of the system-wide :

  • somewhere in , i.e., in
  • in the PostgreSQL installation directory or one of its subdirectories.
  • in the location denoted by the env var or the env var .
  • the location returned when running . Note that not all PostgreSQL installations include .

The location of the user-specific is by default .

On startup, tries to read the system-wide , then the user-specific , if the user-specific exists. Statements in the user-specific have priority over statements in the system-wide .

a sample psqlrc file

This is the psqlrc I use:

\set PROMPT1 '%[%033[1;34;10m%]%n@%m:%>:%/ %#%x%[%033[0m%] ' \x auto \pset null '👻' \set HISTFILE ~/.state/psql/psql_history-:DBNAME \set HISTSIZE 1000 \set HISTCONTROL ignoredups \setenv EDITOR 'nano'

special variables

All variables in have sane default values. The PostgreSQL documentation lists some variable that “are treated specially by psql”. For some of these vars, the value can be set, i.e. for the vars:

  • HISTCONTROL
  • HISTFILE
  • PROMPT1, PROMPT2, PROMPT3

For some of these vars, the value can’t be set, or it doesn’t make sense to set the value of these vars:

  • HOST
  • DBNAME
  • PORT
  • USER

If the value of the variable is to be used to set the value of another psqlrc var, the var must be prepended with the colon , i.e.:

\set HISTFILE ~/.state/psql/psql_history-:DBNAME

configuring the location of psql_history

The default location of this file is . Location and name of the psql history file can be configured either with

configuring the psql prompt

has three prompts:

  • PROMPT1 – normal prompt.
  • PROMPT2 – used when more input is expected during command entry. Like when the obligatory semicolon at the end of a SQL command is omitted.
  • PROMPT3 – used during an SQL command, when it is necessary to type in a row value on the terminal.

A colored prompt in psql helps a lot. To set a colored prompt with text color blue, put the following line into :

\set PROMPT1 '%[%033[1;34;10m%]%n@%m:%>:%/ %#%x%[%033[0m%] '

For user bob, connected to database sampledb by a socket, the resulting prompt will look like this:

bob@[local]:5432:sampledb #

with the prompt in blue foreground color on the terminals default background color. The prompt display items are:

  • %n session username
  • %m database server’s hostname up to the first dot –- is if the connection is over a Unix domain socket
  • %> listening port
  • %/ current database
  • %# whether you’re a superuser (#) or a regular user (>)
  • %x transaction status – usually blank unless in a transaction block (*)

configuring display expansion

can displaying the result of a command in two modes:

  • normal mode – the layout is horizontal. I.e., for a row-set, the columns of the rows are laid out horizontally.
  • expanded mode – the layout is vertical. I.e., for a row-set, the columns of the rows are laid out vertically.

The expanded mode is useful to display single a single row with lots of columns. I do not find expanded mode to be useful for a result with lots of rows or for the result of .

configuring the editor

picks up the editor to use from the env vars , , and . The env vars are evaluated in the order listed here. The first env var which is set is used for editing with the command.

The env var for the editor can be set in the with , i.e. .

configuring the display of null

By default, null values are not displayed in psql output. This means that in the output, the value NULL can not be distinguished from empty strings. The display of NULL can be changed with , i.e.:

\pset null (NULL)
works with anything that is utf-8 compatible, including emojis 🙂
In my psqlrc (see above) I use the ghost emoji.

disabling the psql history file to prevent security scrawl

The psql history may contain sensitive information like a user’s password. To prevent the leaking of such sensitive information, the psql history file can be disabled by

  • linking the psql history file to /dev/null: .
  • setting the env var to /dev/null: .

I have rarely seen this in the wild.

reference

PostgreSQL Client Applications - psql
x