SoFunction
Updated on 2025-04-08

Actions to create notes for tables or views using PostgreSQL

1 Create notes for tables and columns

drop table if exists test;
create table test(
  objectid serial not null,
  num integer not null,
 constraint pk_test_objectid primary key (objectid),
 constraint ck_test_num check(num < 123 ),
);
comment on table test is 'I'm a watch';
comment on column  is 'I'm the only primary key';
comment on column  is 'Quantity Field';
comment on constraint pk_test_objectid on test is 'I am the constraint, the only primary key';
comment on constraint ck_test_num on test is 'I am a constraint, the num field must be less than 123';
\dS+ test;

2 Create notes for views and columns

drop view if exists vtest;
create or replace view vtest
 as select 1 as col1, 'a' as col2, now() as col3;
comment on view vtest is 'View Notes';
comment on column vtest.col1 is 'First column notes, integer type';
comment on column vtest.col2 is 'Second column notes, character type';
comment on column vtest.col3 is 'The third column note, date and time type';

3 comment syntax

COMMENT ON
{
 ACCESS METHOD object_name |
 AGGREGATE aggregate_name ( aggregate_signature ) |
 CAST (source_type AS target_type) |
 COLLATION object_name |
 COLUMN relation_name.column_name |
 CONSTRAINT constraint_name ON table_name |
 CONSTRAINT constraint_name ON DOMAIN domain_name |
 CONVERSION object_name |
 DATABASE object_name |
 DOMAIN object_name |
 EXTENSION object_name |
 EVENT TRIGGER object_name |
 FOREIGN DATA WRAPPER object_name |
 FOREIGN TABLE object_name |
 FUNCTION function_name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] |
 INDEX object_name |
 LARGE OBJECT large_object_oid |
 MATERIALIZED VIEW object_name |
 OPERATOR operator_name (left_type, right_type) |
 OPERATOR CLASS object_name USING index_method |
 OPERATOR FAMILY object_name USING index_method |
 POLICY policy_name ON table_name |
 [ PROCEDURAL ] LANGUAGE object_name |
 PUBLICATION object_name |
 ROLE object_name |
 RULE rule_name ON table_name |
 SCHEMA object_name |
 SEQUENCE object_name |
 SERVER object_name |
 STATISTICS object_name |
 SUBSCRIPTION object_name |
 TABLE object_name |
 TABLESPACE object_name |
 TEXT SEARCH CONFIGURATION object_name |
 TEXT SEARCH DICTIONARY object_name |
 TEXT SEARCH PARSER object_name |
 TEXT SEARCH TEMPLATE object_name |
 TRANSFORM FOR type_name LANGUAGE lang_name |
 TRIGGER trigger_name ON table_name |
 TYPE object_name |
 VIEW object_name
} IS 'text'
where aggregate_signature is:
* |
[ argmode ] [ argname ] argtype [ , ... ] |
[ [ argmode ] [ argname ] argtype [ , ... ] ] ORDER BY [ argmode ] [ argname ] argtype [ , ... ]

Note: There is no COMMENT command in the SQL standard.

Supplement: postgre query comment_PostgreSQL query table and field notes

Query all table names and field meanings

select  Table name,cast(obj_description(relfilenode,'pg_class') as varchar) name, Fields, Fields备注,concat_ws('',,SUBSTRING(format_type(,) from '.∗')) as Column type from pg_class c,pg_attribute a,pg_type t,pg_description d
where >0 and = and = and = and =
and  in (select tablename from pg_tables where schemaname='public' and position('_2' in tablename)=0) order by ,

View all table names

select tablename from pg_tables where schemaname='public' and position('_2' in tablename)=0;
select * from pg_tables;

View table names and notes

select relname as tabname,cast(obj_description(relfilenode,'pg_class') as varchar) as comment from pg_class c
where relname in (select tablename from pg_tables where schemaname='public' and position('_2' in tablename)=0);
select * from pg_class;

View specific table name notes

select relname as tabname,
cast(obj_description(relfilenode,'pg_class') as varchar) as comment from pg_class c
where relname ='Table name';

View specific table name segments

select ,,concat_ws('',,SUBSTRING(format_type(,) from '.∗')) as type, from pg_class c,pg_attribute a,pg_type t,pg_description d
where ='Table name' and >0 and = and = and = and =;

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.