psql Meta-Commands

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/O7 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.
\d[S+] [PATTERN]
\daList aggregate functions.
\da[S] [PATTERN]
\dAList index access methods (btree, hash, gin, gist, brin, spgist, etc.).
\dA[+] [PATTERN]
\dAcList operator classes used by index access methods.
\dAc[+] [AMPTRN [TYPEPTRN]]
\dAfList operator families used by index access methods.
\dAf[+] [AMPTRN [TYPEPTRN]]
\dAoList operators of operator families.
\dAo[+] [AMPTRN [OPFPTRN]]
\dApList support functions of operator families.
\dAp[+] [AMPTRN [OPFPTRN]]
\dbList tablespaces.
\db[+] [PATTERN]
\dcList character set conversions between encodings.
\dc[S+] [PATTERN [PATTERN]]
\dconfigList server configuration parameters and their current values. Added in PostgreSQL 15.
\dconfig[+] [PATTERN]
\dCList type casts.
\dC[+] [PATTERN]
\ddShow comments on objects whose descriptions aren't shown elsewhere (rules, triggers, etc.).
\dd[S] [PATTERN]
\dDList domains.
\dD[S+] [PATTERN]
\ddpList default access-privilege settings configured with ALTER DEFAULT PRIVILEGES.
\ddp [PATTERN]
\dEList foreign tables.
\dE[S+] [PATTERN]
\desList foreign servers.
\des[+] [PATTERN]
\detList foreign tables (same as \dE).
\det[+] [PATTERN]
\deuList user mappings to foreign servers.
\deu[+] [PATTERN]
\dewList foreign-data wrappers.
\dew[+] [PATTERN]
\dfList functions. Filter by kind with letters: a aggregates, n normal, p procedures, t triggers, w window. + shows source code and metadata.
\df[anptwS+] [FUNCPTRN [TYPEPTRN ...]]
\dFList text-search configurations.
\dF[+] [PATTERN]
\dFdList text-search dictionaries.
\dFd[+] [PATTERN]
\dFpList text-search parsers.
\dFp[+] [PATTERN]
\dFtList text-search templates.
\dFt[+] [PATTERN]
\dgList database roles (groups). Identical to \du since PostgreSQL 8.1 unified users and groups.
\dg[S+] [PATTERN]
\diList indexes.
\di[S+] [PATTERN]
\dlList large objects. Same as \lo_list.
\dl[+]
\dLList procedural languages.
\dL[S+] [PATTERN]
\dmList materialized views.
\dm[S+] [PATTERN]
\dnList schemas.
\dn[S+] [PATTERN]
\doList operators with their operand and result types.
\do[S+] [OPPTRN [TYPEPTRN [TYPEPTRN]]]
\dOList collations.
\dO[S+] [PATTERN]
\dpList access privileges on tables, views, and sequences. \z is an alias.
\dp[S] [PATTERN]
\dPList partitioned tables and indexes. Letters: i indexes only, t tables only, n nested partitions.
\dP[itn+] [PATTERN]
\drdsList per-database role settings (ALTER ROLE ... IN DATABASE).
\drds [ROLEPTRN [DBPTRN]]
\drgList role memberships, showing which roles are granted to which. Added in PostgreSQL 16.
\drg[S] [PATTERN]
\dRpList logical replication publications.
\dRp[+] [PATTERN]
\dRsList logical replication subscriptions.
\dRs[+] [PATTERN]
\dsList sequences.
\ds[S+] [PATTERN]
\dtList tables.
\dt[S+] [PATTERN]
\dTList data types.
\dT[S+] [PATTERN]
\duList database roles (users). Same as \dg.
\du[S+] [PATTERN]
\dvList views.
\dv[S+] [PATTERN]
\dxList installed extensions. + shows the objects each extension owns.
\dx[+] [PATTERN]
\dXList extended statistics objects (CREATE STATISTICS).
\dX [PATTERN]
\dyList event triggers.
\dy[+] [PATTERN]
\lList databases on the server, with encoding, collation, and access privileges.
\l[+] [PATTERN]
\sfShow the definition of a function (a re-creatable CREATE FUNCTION statement). + adds line numbers.
\sf[+] FUNCNAME
\svShow the definition of a view.
\sv[+] VIEWNAME
\zList access privileges. Equivalent to \dp.
\z[S] [PATTERN]
Formatting8 commands
\aToggle between aligned and unaligned output mode.
\a
\CSet the table caption (title), or unset it with no argument.
\C [STRING]
\fSet the field separator for unaligned output (default is |).
\f [STRING]
\HToggle HTML table output mode.
\H
\psetSet table-printing options. Common names: border, columns, expanded, fieldsep, footer, format (aligned, asciidoc, csv, html, latex, troff-ms, unaligned, wrapped), linestyle, null, numericlocale, pager, recordsep, tableattr, title, tuples_only.
\pset [NAME [VALUE]]
\tToggle tuples-only mode — suppresses column headers, footers, and the row-count line.
\t [on|off]
\TSet attributes inserted into the HTML <table> tag.
\T [STRING]
\xToggle expanded output, where each column is shown on its own line. auto picks expanded only when the row is too wide for the terminal.
\x [on|off|auto]
Connection4 commands
\cConnect to a new database. Use - to keep the current value for any field. With no arguments, prints the current connection info.
\c[onnect] [DBNAME [USER [HOST [PORT]]]] | conninfo
\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.
\timing [on|off]