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 psql provides 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 with the backslash, these commands are used to perform some important operations in psql without querying the database. There is a long list of meta-commands provided by the psql. 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.
We can see that all the databases in my system have been enlisted upon execution of this command.
\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.
The output advocates that running this command will connect us to the specified database.
\d - Display Objects
The \d meta-command displays all the objects. These objects can be a table, view, index, schema, and database. We will see how can we specifically get them.
- \dt - Display Tables
This meta-command displays all the tables in the database when it is used as “\dt”.
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.
- \di - Display Indexes
To display indexes, we use the \di command.
- \dn - Display Schemas
The “\dn” command will give the list of schemas present in the system.
- \dT - Display the Data Types
The \dT command displays the data types in the system.
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.
- \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.
- \du - Displays all the Roles in the Database
This meta-command gives all the user roles defined in the system.
In my case, the only user role defined is the Postgres.
\x - Set the Expanded Display
We can toggle the expanded display using this command. This is useful for large tables. We can set it to auto, it can be toggled on or off.
\set - Display Internal Variables
The \set command is used to list all the internal variables.
\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.
\? - get help
This command is used to get help from the psql, particularly regarding meta-commands.
\a - Toggles Alignment of the Output Format
The “\a” command aligns and unaligns the output format.
\C<caption> - Sets the Title/Caption of a Table
The “\C<caption>” command sets the title for the database table.
\r - Resets the Query Buffer
This command resets the query buffer.
\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.
\q - Quit psql
By running the “\q” query you can quit the psql. The execution will give:
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. Such as;
In the above example, we have used the 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:
While if we execute the “\dt+” command.
There is some additional information displayed.
Note: The meta-commands never end with the semi-colon. If we add a semi-colon at the end of the meta-command, the meta-command will either do nothing or will throw an error.
That was all about the meta-commands.
Conclusion
The meta-commands are the commands offered by psql to perform certain operations. These commands start with the backslash and do not end with the semicolon. In this post, we have seen many meta-commands that are being used widely.