How to create a dynamic comment to an object? I want to embed variable information in a table that is created from a script. The following works:

SELECT( 'created ' || CURRENT_DATE ||' and is in CH1903 LV03');


But it seems I can't use it for creating a comment:

COMMENT ON TABLE public.mytable IS SELECT( 'created ' || CURRENT_DATE ||' and is in CH1903 LV03');
\set mycomment SELECT( 'created ' || CURRENT_DATE ||' and is in CH1903 LV03');
COMMENT ON TABLE public.mytable IS :mycomment; 


Is dynamic SQL the only option?
Here is a very simple example of such a function: 

create or replace function comment_on(p_type text, p_object text, p_comment text)
  returns text
as
$$
declare 
  l_sql     text;
  l_comment text;
begin
  l_comment := replace(p_comment, '${created}', 'created at '||to_char(current_date, 'yyyy-mm-dd'));
  l_sql := 'comment on '||p_type||' '||p_object||' is '||quote_literal(l_comment);
  execute l_sql;
  return l_comment;
end;
$$
language plpgsql;


You can of course add more placeholders to also support e.g. the current user or similar things (or always append this to the comment regardless of the placeholder):

Note that it lacks any error handling or prevention of SQL injection (but that is probably not a problem). 

Usage is like this:

create table foo (id integer);
select comment_on('table', 'stuff.foo', '${created} stores bar data');
select comment_on('column', 'stuff.foo.id', 'the id column');


The table foo will then have the following comment:

List of relations
 Schema | Name | Type  | Owner    |  Size     |              Description
--------+------+-------+----------+-----------+---------------------------------------
 public | foo  | table | postgres | 0 bytes   | created at 2015-10-27 stores bar data
(1 row)


And the column will have the following comment:

                           Table "public.foo"
 Column |  Type   | Modifiers | Storage | Stats target |  Description
--------+---------+-----------+---------+--------------+---------------
 id     | integer |           | plain   |              | the id column