Backslash commands for the Postgres interactive terminal — informational listings, query buffer control, formatting, conditionals, and more.
⌕
General11 commands
\copyrightShow PostgreSQL usage and distribution terms.
\copyright
\crosstabviewExecute the current query buffer and display the result as a crosstab grid with column headers from one column and row values from another.
\crosstabview [colV [colH [colD [sortcolH]]]]
\errverboseRepeat the most recent server error message at the maximum verbosity, as if VERBOSITY was set to verbose and SHOW_CONTEXT to always.
\errverbose
\gSend the current query buffer to the server. Optionally write results to a file or pipe them to a shell command. Per-call \pset options can be passed in parentheses.
\g [(option=value [...])] [filename | |command]
\gdescShow the column names and types that the current query would produce, without executing it.
\gdesc
\getenvFetch the value of an environment variable into a psql variable.
\getenv psql_var env_var
\gexecExecute the current query, then send each cell of the result back to the server as a query. Useful for dynamic SQL generation.
\gexec
\gsetExecute the current query and store the result columns into psql variables, optionally prefixed.
\gset [prefix]
\gxEquivalent to \g but forces expanded output mode for this one query.
\gx [(option=value [...])] [filename | |command]
\qQuit psql. Ctrl+D at an empty prompt does the same.
\q
\watchRe-execute the current query buffer on a fixed interval until interrupted. Optional count limits iterations; min_rows stops when fewer rows are returned.
\watch [[i=]seconds] [c=count] [m=min_rows]
Help2 commands
\?Show help. With no argument, list backslash commands. Use options for command-line flags or variables for psql special variables.
\? [commands | options | variables]
\hShow syntax help for a SQL command. With no argument, lists all SQL commands.
\h [NAME]
Query Buffer7 commands
\eOpen the query buffer (or an external file) in $EDITOR. After editing, the buffer is re-loaded but not executed until you submit it.
\e [FILE] [LINE]
\efEdit a function definition in $EDITOR. With no name, opens a CREATE FUNCTION template.
\ef [FUNCNAME [LINE]]
\evEdit a view definition in $EDITOR. With no name, opens a CREATE VIEW template.
\ev [VIEWNAME [LINE]]
\pPrint the current contents of the query buffer.
\p
\rReset (clear) the query buffer.
\r
\sDisplay the command history, or save it to a file. Requires GNU Readline support.
\s [FILE]
\wWrite the current query buffer to a file.
\w FILE
I/OInput / Output7 commands
\copyClient-side COPY. Streams data between a SQL table and a file (or program) on the client host — works without server filesystem access or superuser privileges.
\copy { table | (query) } { from | to } { filename | stdin | stdout | program 'cmd' } [options]
\echoWrite the given string to standard output. -n suppresses the trailing newline.
\echo [-n] [STRING ...]
\iRead and execute SQL commands from a file.
\i FILE
\irLike \i, but the path is resolved relative to the directory of the currently-running script.
\ir FILE
\oRedirect future query results to a file, a pipe to a shell command, or (with no argument) back to standard output.
\o [FILE | |COMMAND]
\qechoLike \echo, but writes to the current query-output channel (the file set by \o).
\qecho [-n] [STRING ...]
\warnLike \echo, but writes to standard error instead of standard output.
\warn [-n] [STRING ...]
Conditional4 commands
\ifBegin a conditional block. The expression is evaluated as a boolean (true/yes/on/1 are true). Commands inside are skipped when the condition is false.
\if EXPRESSION
\elifAlternate branch in a conditional block, evaluated only if no earlier branch matched.
\elif EXPRESSION
\elseDefault branch in a conditional block, taken only if no \if or \elif branch matched.
\else
\endifEnd the current conditional block.
\endif
Informational50 commands
\dDescribe a table, view, materialized view, index, sequence, or foreign table. With no pattern, lists all visible relations. S includes system objects; + adds detail.
\conninfoShow the current connection's database, user, host, port, and SSL status.
\conninfo
\encodingShow or set the client character encoding.
\encoding [ENCODING]
\passwordChange a role's password. The new password is prompted for and hashed client-side before being sent, so it never appears in plaintext on the wire or in server logs.
\password [USERNAME]
Large Objects4 commands
\lo_exportWrite a large object's contents to a file on the client host.
\lo_export LOBOID FILE
\lo_importRead a file on the client host into a new large object. Returns the assigned OID.
\lo_import FILE [COMMENT]
\lo_listList large objects (same as \dl).
\lo_list[+]
\lo_unlinkDelete a large object.
\lo_unlink LOBOID
Variables5 commands
\promptPrompt the user for input and store it in a psql variable.
\prompt [TEXT] NAME
\setSet a psql variable. With no arguments, list all variables. Many special variables (AUTOCOMMIT, ON_ERROR_STOP, ECHO, VERBOSITY, etc.) change psql behavior.
\set [NAME [VALUE]]
\unsetUnset (delete) a psql variable.
\unset NAME
\bindSet positional parameters ($1, $2, …) for the next query, which will be executed via the extended-query protocol. Added in PostgreSQL 16.
\bind [PARAMETER ...]
\bind_namedBind parameters and execute a previously-prepared statement by name. Added in PostgreSQL 17.
\bind_named STATEMENT_NAME [PARAMETER ...]
OS / Misc4 commands
\!Run a shell command (or open an interactive subshell with no argument). The current working directory is whatever psql's is.
\! [COMMAND]
\cdChange psql's working directory. With no argument, changes to the user's home directory.
\cd [DIRECTORY]
\setenvSet an environment variable in psql's environment, or unset it with no value.
\setenv NAME [VALUE]
\timingToggle whether the duration of each SQL command is reported after the result.