Updated on 2025-10-23 GMT+08:00

COMMENT

Description

Defines or changes the comment of an object.

Precautions

  • Each object stores only one comment. Therefore, you need to modify a comment and issue a new COMMENT command to the same object. To delete the comment, write NULL at the position of the text string. When an object is deleted, the comment is automatically deleted.
  • Currently, there is no security protection for viewing comments. Any user connected to a database can view all the comments for objects in the database. For shared objects such as databases, roles, and tablespaces, comments are stored globally so any user connected to any database in the cluster can see all the comments for shared objects. Therefore, do not put security-critical information in comments.
  • To comment objects, you must be an object owner or user granted the COMMENT permission. By default, system administrators have the permission.
  • Roles do not have owners, so the rule for COMMENT ON ROLE is that you must be an administrator to comment on an administrator role, or have the CREATE ROLE permission to comment on non-administrator roles. A system administrator can comment on all objects.

Syntax

COMMENT ON
{ AGGREGATE func_name ([arg [, ...]])|
COLLATION object_name |
  COLUMN { table_name.column_name | view_name.column_name } |
  CONSTRAINT constraint_name ON table_name |
  DATABASE object_name |
  FUNCTION func_name ([arg [, ...]])|
  INDEX object_name |
  SEQUENCE seq_name |
  USER user_name |
  ROLE object_name |
  SCHEMA object_name |
  TABLE object_name |
  TABLESPACE object_name |
  VIEW object_name |
}
   IS 'text';

Parameters

  • func_name

    Specifies the name of a function.

  • arg

    Specifies the function variable.

  • user_name

    Value range: an existing username. For details about the username requirements, see •user_name.

  • object_name

    Specifies the name of an object.

  • table_name.column_name

    view_name.column_name

    Specifies a column name. You can add the table name or view name as the prefix.

  • constraint_name

    Specifies the name of a table constraint.

  • table_name

    Specifies the name of a table.

  • text

    Specifies the comment content.

Examples

-- Create a table.
m_db=# CREATE TABLE emp(
    empno varchar(7),
    ename varchar(50),
    job varchar(50),
    mgr varchar(7),
    deptno int
);
-- Add comments to a table.
m_db=# COMMENT ON TABLE emp IS 'Department table';
-- Add comments to columns.
m_db=# COMMENT ON COLUMN emp.empno  IS  'Employee ID';
m_db=# COMMENT ON COLUMN emp.ename  IS  'Employee name';
m_db=# COMMENT ON COLUMN emp.job  IS  'Job';
m_db=# COMMENT ON COLUMN emp.mgr  IS  'Manager ID';
m_db=# COMMENT ON COLUMN emp.deptno  IS  'Department ID';

-- View table comments.
m_db=# \d+
 Schema |    Name     |   Type   | Owner |    Size    |             Storage              | Description 
--------+-------------+----------+-------+------------+----------------------------------+-------------
 public | emp         | table    | omm   | 0 bytes    | {orientation=row,compression=no} | Department table

-- View column comments.
m_db=# \d+ emp
                                 Table "public.emp"
 Column |         Type          | Modifiers | Storage  | Stats target | Description 
--------+-----------------------+-----------+----------+--------------+-------------
 empno  | character varying(7)  |           | extended |              | Employee ID
 ename  | character varying(50) |           | extended |              | Employee name
 job    | character varying(50) |           | extended |              | Job
 mgr    | character varying(7)  |           | extended |              | Manager ID
 deptno | integer               |           | plain    |              | Department ID
Has OIDs: no
Options: orientation=row, compression=no, storage_type=USTORE

-- Delete the emp table.
m_db=# DROP TABLE emp;