A Comprehensive Guide on psql Meta-Commands

Meta commands are the instructions given to the client engine/psql by the user to display some functionalities or perform certain operations. These commands are also called the “slash or backslash command” because they begin with a backslash followed by some command verb. Arguments can also be specified in the meta-commands. The SQL Shell (psql) supports a long list of meta-commands.

This tutorial will demonstrate the most frequently used meta-commands in psql.

A Comprehensive Guide on psql Meta-Commands

The meta-commands are small commands written/prefixed with the backslash. These commands are used to perform different operations like managing users, databases, tables, etc. There is a long list of meta-commands provided by the psql and a few of them are listed below:

We will go over them one by one with the implementation.

\l - Lists All the Databases

The \l meta-command lists all the databases in the system. Let's see how it works:

\l

Important: The \l command must be executed without a semi-colon; otherwise, you wouldn't get the desired results.

\c<database_name> - Connect to a Specific Database

The \c<database_name> command is used to get connected to the database whose name is specified in the command:

\c test_db
img

The output demonstrates that running the "\c" command connects us to the specified database, i.e., "test_db".

\d - Display Objects

The \d meta-command displays all the objects. These objects include a table, view, index, schema, and database. We will implement the \d command with different options to display different database objects.

  • \dt - Display Tables

This meta-command displays all the tables in the database when it is used as “\dt”:

\dt
img

The above enlisted are the tables available in my default database.

  • \dv - Display Views

This meta-command will give us all the views present in our database. In my case, I have never created any view so the command will find no such relation. The \dm is used to display the materialized view. The same will be the output for this case:

\dv;
\dm;
img
  • \di - Display Indexes

To display indexes, we use the \di command as follows:

\di
img
  • \dn - Display Schemas

The “\dn” command will give the list of schemas present in the system:

\dn
img
  • \dT - Display the Data Types

The \dT command displays the user-defined data types:

\dT

Note that the “\dt” is different from “\dT”. The \dt displays tables.
  • \ds - Display the Sequences

The “\ds” command displays all the sequences of a database.

img
  • \dtS - Displays System Catalog Tables

The system catalog tables consist of the tables and views and describe the structure of the database. These can be displayed in the psql command line using the \dtS command:

\dtS
img
  • \du - Displays all the Roles in the Database

This meta-command gives all the user roles defined in the system:

\du
img

In my case, the only user role defined is the Postgres.

\x - Set the Expanded Display

We can toggle the expanded display using the "\x" command. This is useful for large tables. We can set it to auto or can be toggled on or off:

\x
img

\set - Display Internal Variables

The \set command is used to list all the internal variables, as shown below:

\set
img

\unset <name_of_variable>- Delete an Internal Variable

This command will delete the internal variable from the list.

\echo<text> - Print Message

This command is used to get the text/message printed to the console:

\echo Hello From Command Prompt!
img

\? - get help

This command is used to get help from the psql, particularly regarding meta-commands:

\?
img

\a - Toggles Alignment of the Output Format

The “\a” command aligns and unaligns the output format, as demonstrated in the following snippet:

\a
img

\C<caption> - Sets the Title/Caption of a Table

The “\C<caption>” command sets the title for the database table:

\C hello
img

\r - Resets the Query Buffer

This command resets the query buffer:

\r
img

\z - Lists All the Tables With Their Revoked Permissions

The “\z” command displays the list of tables with their objects as Access Control Lists i.e. ACLs:

\z
img

\q - Quit psql

By running the “\q” query you can quit the psql, as illustrated in the following snippet:

\q
img

Now by pressing any key, you can simply exit from the psql.

Additional Information

You can execute multiple meta-commands at the same time to perform the desired operation like this:

img

In the above example, we have used three commands to display the table caption, and the table, and toggle the alignment respectively and simultaneously.

We can add a “+” sign to every command to get some additional technical information about whatever is the output. For example; running the “\dt” gives:

img

While if we execute the “\dt+” command. There is some additional information displayed.

img

That's all the essential details regarding the psql meta-commands.

Conclusion

The meta-commands are the commands offered by psql to perform certain operations. These commands start with the backslash and are executable only in psql. In this post, we have seen many meta-commands that are being used widely.