PostgreSQL | PostgreSQL Basic Syntax | Writing Comments
This page explains how to write comments when writing SQL in PostgreSQL.
How to Write Comments
When writing an SQL statement, if you want to add a comment, write two consecutive hyphens (--). Everything from that point to the end of the line is treated as a comment.
-- This whole line is a comment.
To write a multi-line comment, the part from /* to */ is treated as a comment.
/* The comment starts here.
Multiple lines of comments are possible.
*/
When executed, comments are completely ignored. If you execute SQL commands from the psql command line, comments have no effect, but they are useful when SQL commands are written in another file and then loaded and executed.
Try it in practice. Open a text editor and write the following.
/*
Create a table and add data
2020/10/26
*/
-- Create table
create table friends (id integer, name varchar (10));
-- Add data
insert into friends values (1, 'kimkc');
insert into friends values (2, 'hwang.yh');
insert into friends values (5, 'lim.yt');
Save the file in the c:\dev directory with the name test.sql.
The current character encoding of psql is UHC, so save the file using the EUC_KR character encoding.
postgres=# \encoding
UHC
postgres=#
Then connect to the mydb database in PostgreSQL using psql.
C:\Users\kimkc>psql -U postgres -d mydb
Password for user postgres:
psql (12.2)
Type "help" for help.
mydb=#
To load and execute the saved file, use the \i psql command as follows.
mydb=# \i c:/dev/test.sql
CREATE TABLE
INSERT 0 1
INSERT 0 1
INSERT 0 1
mydb=#
The comment parts written in the file were ignored, and the other parts were executed successfully. To confirm, run the following SQL command in psql.
mydb=# select * from friends;
id | name
----+----------
1 | kimkc
2 | hwang.yh
5 | lim.yt
(3 rows)
You can confirm that the table was created and three rows of data were added.
–
This page explained how to write comments in PostgreSQL.