58

What's the character for comments for special commands that start with \ in psql?

The following appears to work:

SELECT * FROM my_table     -- this is my comment

But this does not work:

\dt jvcurve_thin.jvcurve_results    --  my comment #2

\dt: extra argument "--" ignored

2
  • 12
    psql doesn't support trailing-line comments on its slash commands. Just put the comment on the line before. Dec 25, 2013 at 9:13
  • 1
    The above response should be posted as an answer, not a comment. This will allow voting and follow-up comments. In particular, I recommend referring to cjerdonek's answer.
    – David J.
    Aug 30, 2021 at 13:44

5 Answers 5

76

According to PostgreSQL documentation, there are both the inline and the block style comments.

The inline style:

SELECT 23 AS test  -- this is just a test

The block style:

/* The following is a very
 * non-trivial SQL code */
SELECT 42 AS result
2
  • 4
    He asks how to comment in psql interactive console scripts (i.e. include commenting its backslashed meta-commands, which is the trick), not how comment the regular Postgres SQL. Sep 4, 2016 at 22:16
  • @PetrPivonka The title of this question "Comment character/characters in postgres / postgresql / psql?" is how the search engines primarily index it.
    – Tregoreg
    Nov 23, 2019 at 2:02
22

In SQL comment starts with --.

3
  • 4
    C-style comments are also allowed by most databases but I'm not sure which (if any) SQL standard stipulates them: /* comment */ Nov 25, 2010 at 0:02
  • Historical context: this answer was useful at the time; the OP thanked the author. Afterwards, the OP asked a follow-up question about how to put comments in psql meta-commands (e.g. \l for list databases). At this point in time, only cjerdonek's answer has answered these follow-up questions.
    – David J.
    Aug 30, 2021 at 13:43
  • 1
    The weird part is, the follow-up question wasn't even the original author. Someone else just edited the question to ask a different question instead. Nov 7, 2022 at 17:41
7

It doesn't look like psql supports traditional end-of-line -- comments in its psql-specific "slash commands." -- these are called meta-commands in the PostgreSQL psql documentation.

However, if you're okay with the end-of-line comments being displayed on execution, using \echo seems to be a valid work-around. For example:

\dt jvcurve_thin.jvcurve_results   \echo my comment #2

The "double slash" separator meta-command looks like another possibility (and without the side effect of echoing). Begin a new command with it and immediately start a -- comment:

\dt jvcurve_thin.jvcurve_results   \\ -- my comment #2

Finally, switching to the shell and adding a shell comment seems like another possibility:

\dt jvcurve_thin.jvcurve_results   \! # my comment #2
4
  • You probably shouldn't use two backslashes \\ since these are used to separate commands for psql, e.g. \d table1 \\ \d table2, and can exhibit behaviour when the supposed comment includes psql commands. See description of the option -c here.
    – valid
    Feb 28, 2014 at 13:33
  • Thanks, @valid. How about prepending the text after \\ with -- then, as in \\ -- my comment.
    – cjerdonek
    Mar 1, 2014 at 18:51
  • 2
    oh yeah. That seems alright. It still introduces some different behaviour. When typing double-dash into psql interactive console that comment command and those directly following are collected and stored into history with the next (comment-breaking) non-comment command - quasi behaving like a multiline comment. Double-backslash combined with double-dash doesn't work in the same way, i.e. the comment is not compiled with the following command(s). Even though I'd consider all of this wanted behaviour I don't find it particularly intuitive. So be aware ;)
    – valid
    Mar 2, 2014 at 15:56
  • @valid Interesting. I guess in the case of double-slash followed by a double-hyphen comment, it could be argued that it makes sense for an end-of-line comment to be associated only with that line (because it is an end-of-line comment).
    – cjerdonek
    Mar 2, 2014 at 22:47
6

From the official documentation: PostgreSQL Comments.

A comment is a sequence of characters beginning with double dashes and extending to the end of the line, e.g.:

-- This is a standard SQL comment

Alternatively, C-style block comments can be used:

/* multiline comment  * with nesting: /* nested block comment */  */

where the comment begins with /* and extends to the matching occurrence of */. These block comments nest, as specified in the SQL standard but unlike C, so that one can comment out larger blocks of code that might contain existing block comments.

A comment is removed from the input stream before further syntax analysis and is effectively replaced by whitespace.

And it has been supported the same way ever since dark ages (version 7.0).

-3

It's Better to select and press Ctrl+Shift+/ to comment multiline in PostgreSQL pgAdmin4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.