Comment character/characters in postgres / postgresql / psql?

PostgresqlCommentsPsql

Postgresql Problem Overview


What's the character for comments in postgres?

SELECT * FROM my_table     # pound  sign produces a syntax error

Thank you cababunga, 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

Postgresql Solutions


Solution 1 - Postgresql

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

Solution 2 - Postgresql

In SQL comment starts with --.

Solution 3 - Postgresql

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).

Solution 4 - Postgresql

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

Solution 5 - Postgresql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionrussian_spyView Question on Stackoverflow
Solution 1 - PostgresqlTregoregView Answer on Stackoverflow
Solution 2 - PostgresqlcababungaView Answer on Stackoverflow
Solution 3 - Postgresqlvitaly-tView Answer on Stackoverflow
Solution 4 - PostgresqlcjerdonekView Answer on Stackoverflow
Solution 5 - PostgresqlSaad View Answer on Stackoverflow