Sub topic 3
Meta-commands and Built-in Help
Assume you know basic SQL, can use a terminal, and have access to a PostgreSQL database through psql. This chapter focuses on commands that help you explore a database. These commands do not query tables directly. They tell the psql client what to display or do.
SQL commands and meta-commands
SQL is sent to the PostgreSQL server. A psql meta-command is handled by the client before it sends anything to the server. Every meta-command begins with a backslash.
SQL statements usually end with a semicolon. Meta-commands do not need a semicolon.
SELECT current_database();
\conninfo
\dtThe first line asks the server for the current database. The other lines ask psql to show connection and table information.
Discovering available commands
Use \? to list the main groups of psql meta-commands:
\?The output includes commands for databases, schemas, tables, formatting, connections, and help. To find help for SQL syntax, use \h or \help.
\h SELECT
\h CREATE TABLEThe help text shows the syntax supported by your PostgreSQL server. This is useful when you need to check optional clauses or the exact form of a statement.
Use \? with a topic to inspect client options when supported by your psql version:
\? optionsYou can also ask your operating system for help about starting psql:
psql --helpThat help describes connection flags such as -h, -p, -U, and -d. The \h command describes SQL statements, while \? describes psql commands.
Common exploration commands
The following commands are useful when you enter an unfamiliar database.
| Command | Purpose |
|---|---|
| `\conninfo` | Shows the current server, port, database, user, and SSL status |
| `\l` | Lists databases |
| `\c mydb` | Connects to the database named `mydb` |
| `\dn` | Lists schemas |
| `\dt` | Lists tables visible in the search path |
| `\dt public.*` | Lists tables in the `public` schema |
| `\dv` | Lists views |
| `\di` | Lists indexes |
| `\df` | Lists functions |
| `\d orders` | Describes the `orders` table |
| `\d+ orders` | Describes the table with additional details |
A pattern such as public.* limits the result to one schema. The * matches any object name.
\conninfo
\dn
\dt public.*
\d+ orders
\dv public.*
\di public.*
\df public.*The \d command can describe more than tables. For example, \d order_summary displays information about a view if that object exists. \d without a name lists relations that match the default display rules.
Reading and correcting commands
At the psql prompt, a line ending with a semicolon runs the SQL statement. A line without a semicolon keeps building the statement until you finish it.
Use \p to print the query currently being built:
SELECT *
FROM orders
WHERE customer_id = 42
\pUse \r to clear the unfinished query buffer. Use \q to leave psql.
\r
\qThe prompt changes when a statement is incomplete. A prompt such as mydb-# means psql is waiting for more SQL. A prompt such as mydb=# means it is ready for a new command.
A short exploration routine
When you connect to an unfamiliar database, run this sequence:
\conninfo
\l
\dn
\dt public.*
\d+ orders
\h SELECTFirst confirm where you are. Then inspect the available databases, schemas, and tables. Finally, describe a table and look up SQL syntax as needed.
Exercise
Which command lists tables in the public schema, and which command describes a table named orders with extra detail?
Show answer
Use \dt public.* to list the tables and \d+ orders to describe the orders table with extra detail.