How to Comment in PostgreSQL

Comments play a very important role in PostgreSQL. Users add comments along with the commands/queries to make them more readable and understandable. At the time of execution, the interpreter completely ignores them. So, using comments, users can write anything that assists them in understanding their code/statements.

Postgres supports single-line as well as multi-line comments. This write-up will demonstrate both of them via practical examples. So, let’s start with the importance of comments in Postgres.

Importance of Comments

As discussed earlier, the comments in PostgreSQL make the commands more understandable. Let’s consider the below-listed points to understand the importance of comments in a better way:

  • Comments make the statements easy to read.
  • Comments assist the users in error detection and code maintenance.
  • Comments are used to describe a function, query, statement, etc.

Let’s explore various types of comments and how to use them in Postgres via practical examples.

How to Comment in PostgreSQL?

PostgreSQL facilitates us with single-line as well as multi-line comments. Let’s comprehend each of them one by one via practical examples.

Single-Line Comment in Postgres

In PostgreSQL, the double hyphen “--” sign is used for single-line comments. The below syntax will provide you with more clarity regarding single-line comments in Postgres:

– write anything here

Multi-Line Comment in Postgres

To use multi-line comments in Postgres, follow the syntax given below:

/* write anything here */

The above snippet indicates that to use multi-line comments, you need to enclose the comments within “/*” and “*/”.

Example 1: How to Use a Single Line Comment in PostgreSQL?

Postgres single-line comments are demonstrated in this example program:

-- Creating a new table using CREATE TABLE command
CREATE TABLE cmt_example(
id INT, --creating an INTEGER type column
name TEXT --creating a character type column
);
img

The table named cmt_example has been created. It shows that the interpreter executes the commands and ignores the single-line comments.

Example 2: How to Use Multi-Line Comments in PostgreSQL?

Here is an example of commenting on multiple lines in Postgres:

/*
CREATE TABLE cmt_example(
id INT, --creating an INTEGER type column
name TEXT --creating a character type column
);
*/
INSERT INTO cmt_example(id, name)
VALUES (1, 'single-line comment'),
(2, 'Multi-line comment');
img

The output shows that the query enclosed within the multi-line comments didn’t execute. While the rest of the commands get executed and perform the functionality accordingly.

Conclusion

PostgreSQL supports single-line as well as multi-line comments. The double hyphen “--” sign is used for single-line comments, while to use multi-line comments, you need to enclose the comments within “/*” and “*/”. Comments make the statements easy to read, more understandable, describe commands, etc. This write-up demonstrated the working of single-line and multi-line comments via practical examples.