How to Comment Characters in psql

In order to bring the proper clarity and to make the code readable, comments are added to the code. Comments are added by the developer or the programmer of the code to make it understandable for everyone. We also usually use comments in the psql so that the queries and commands are more understandable.

We can simply comment in the psql by using “--” for one line code and “\* and */” for multiliner code. But what if we wish to add/insert some special characters to the code? How can they be commented so that they are correctly interpreted by the SQL Shell? Let’s get started with the article to find the answer to all these queries.

How to Comment Characters in SQL Shell (psql)?

Comments are ignored by the psql for the query execution. These comments are only written by the programmer to make the queries understandable. If we want to write a single-line comment, it will be written as follows:

-- This is a Command Prompt --single line comment--

For the multi-line comment, we write them as:

/* This is Command prompt &&
  This is an example of multiple lines comments */

We can see that in both the above syntaxes, special characters are part of the comment. This thing works perfectly in the pgAdmin or any other GUI-based platform that works on PostgreSQL.

But these might cause some problems in CLIs like psql. Let’s see how commenting in this way can be an issue in psql.

Let’s consider the following query:

SELECT * FROM test_scores; -- select the table

In the above code block, we have written a query to select the table name “test_scores”. We have also added a single-line comment with it. This query will work fine i.e. ignore the comment for the query execution. The output looks like this:

img

The comments work well for queries like these but if we write the queries or meta-commands with a slash, the queries return an error. Consider the following example to understand this concept better:

\dt -- display tables

It gives an error in the output that:

img

This does not only happen in the case of the “dt” command. In the case of any meta-command, starting with a slash would similarly result in an error.

This issue can be resolved in the following ways:

Method 1: Running the Comment Before the Query

To resolve the above issue, we can run the comment before the query rather than running it or writing it above the query. Since the comments are only meant to show what is happening in the code and to add some clarity. We can implement this method like this:

postgres=# -- display tables!
postgres=# \dt

Now the query will execute perfectly fine.

img

So in this way, we can handle this case.

Method 2: Using \ECHO

The “\echo” command is used to print the statement specified after it on the psql. We can use this meta-command to make comments in the psql. This method can be implemented like this:

\dt \echo display_all_the_tables!

This method will also work fine. The query will also run as it is meant to be and the comment is also added without causing any problem in it.

img

In this way, we have printed the comment using the \echo command.

Method 3: Using \WARN

Another way to comment characters in psql is using the \warn command. This approach is almost similar to the one given above. The \warn meta-command warns or prints some error. This method can be implemented like this:

\dt \warn display_the_table!

This query will return the following output on the psql:

img

The warning is printed after the execution of the command as a comment.

Method 4: Using \\ and - -

In this method, we first write out the query then we add a separator i.e. “\\”, and then we will write the comment using - -. The query and the comment are separated by the separator. The comment in this case is not printed as an output of executing the query. Let’s implement this method.

img

This is how this method is implemented.

Method 5: Using COMMENT Command

The last and one of the effective methods used to comment in psql is by utilizing the COMMENT command. This command is used to comment on specific database objects we can use this command to comment on the table like this:

COMMENT on table test_scores is 'Amazing Results!';

Executing this command will give the following output:

img

Now to see the comment we will execute the command to display the table in its detailed form. The query is “\d+”. This query will show us the comment i.e.

img

We can see that the comment has been added to the table as a description.

So these all are the methods we can use for commenting the special characters in psql that may be brought into use as per our needs.

Conclusion

Commenting on the special character in psql may be a problem sometimes. To avoid errors we can use different methods. The first one is by “running the comment before the query” to avoid the error. To add comments we can also use \echo and \warn commands, these commands print the comment. The third method is to separate the query/command and the comment using the “\\” separator. Lastly, we can comment on some specific database objects using the COMMENT command. This article has covered all the methods in detail with practical implementation.