Skip to main content
0 reactions

How to Get Help in psql

Featured card titled Get Help in psql, with a terminal window listing four ticked help commands: help, \?, \? variables and \h CREATE TABLE.
The help commands built into psql.

You don’t need to remember every psql command or SQL keyword: psql has help for both built in. This guide shows the few commands that bring it up, and what each one shows.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxPlain text
help
\? [ commands | options | variables ]
\h [ command ]
  • help: a short summary of where to find help.
  • \?: lists psql’s own backslash commands. Add options for psql’s command-line options, or variables for its settings.
  • \h: help for SQL. On its own it lists every SQL command, and with a command’s name it shows that command’s syntax.

Both are described in the psql documentation (opens in a new tab).

Step 1: Start with help

Connect with psql, as Part 2 of the PostgreSQL Tutorial shows, and type:

postgres=#Plain text
help
OutputPlain text
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

Step 2: List psql’s Commands with \?

postgres=#Plain text
\?
Output (the first lines)Plain text
General
  \copyright             show PostgreSQL usage and distribution terms
  \crosstabview [COLUMNS] execute query and display result in crosstab
  \errverbose            show most recent error message at maximum verbosity
  \g [(OPTIONS)] [FILE]  execute query (and send result to file or |pipe);
                         \g with no arguments is equivalent to a semicolon
  \gdesc                 describe result of query, without executing it
  \gexec                 execute query, then execute each value in its result
  \gset [PREFIX]         execute query and store result in psql variables
  \gx [(OPTIONS)] [FILE] as \g, but forces expanded output mode
  \q                     quit psql

The list is longer than your window, so psql shows one screenful at a time with a : on the last line. Press Space for the next screenful, or q to go back to the prompt.

Step 3: Look Up psql’s Settings

postgres=#Plain text
\? variables
Output (the first lines)Plain text
List of specially treated variables

psql variables:
Usage:
  psql --set=NAME=VALUE
  or \set NAME VALUE inside psql

  AUTOCOMMIT
    if set, successful SQL commands are automatically committed
  COMP_KEYWORD_CASE
    determines the case used to complete SQL key words

Each setting in the list has a line or two explaining what it does.

Step 4: List Every SQL Command

postgres=#Plain text
\h
Output (the first lines)Plain text
Available help:
  ABORT                            CREATE FOREIGN TABLE             DROP SCHEMA
  ALTER AGGREGATE                  CREATE FUNCTION                  DROP SEQUENCE
  ALTER COLLATION                  CREATE GROUP                     DROP SERVER
  ALTER CONVERSION                 CREATE INDEX                     DROP STATISTICS
  ALTER DATABASE                   CREATE LANGUAGE                  DROP SUBSCRIPTION

Step 5: Show the Syntax of One SQL Command

postgres=#Plain text
\h CREATE TABLE
Output (the lines that matter)Plain text
Command:     CREATE TABLE
Description: define a new table
Syntax:
CREATE [ persistence_mode ] TABLE [ IF NOT EXISTS ] table_name ( [
…
URL: https://www.postgresql.org/docs/19/sql-createtable.html

The last line links the matching page of the PostgreSQL documentation, which explains every part of the syntax. The name doesn’t have to be complete: \h SELEC shows the help for SELECT.

Step 6: Get Help Before You Start psql

In Terminal, psql lists its command-line options:

TerminalBash
psql --help
Output (the first lines)Plain text
psql is the PostgreSQL interactive terminal.

Usage:
  psql [OPTION]... [DBNAME [USERNAME]]

General options:
  -c, --command=COMMAND    run only single command (SQL or internal) and exit
  -d, --dbname=DBNAME      database name to connect to
  -f, --file=FILENAME      execute commands from file, then exit
  -l, --list               list available databases, then exit
  -v, --set=, --variable=NAME=VALUE
                           set psql variable NAME to VALUE

Common Mistakes

MistakeWhat happensDo this instead
Typing help SELECTpsql treats it as the start of a SQL statement, and the prompt changes to postgres-#Use \h SELECT. help works only on its own.
Asking for a command psql doesn’t know, such as \h FOONo help available for "FOO". Try \h with no arguments to see available help.Check the spelling, or run \h to see every name.

FAQ

What’s the difference between \? and \h?

\? is about psql itself: its backslash commands, its options and its settings. \h is about SQL, the language the server understands.

Where do I find more than the one-line descriptions?

In the psql documentation (opens in a new tab), which explains every backslash command in full. For SQL commands, follow the URL at the end of the \h output.

Conclusion

Type help for a summary, \? for psql’s commands and settings, and \h for SQL syntax. Together they answer most questions without leaving psql.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments