Use `\gset` in psql to store the result of a query in a local variable - like dezso already advised. Shorter yet, `\gset` can replace the semicolon (`;`) as query terminator: test=# SELECT count(*) AS list FROM department.master_data \gset Then, to get a single-quoted string (with any contained single quotes safely escaped automatically) refer to the psql variable with this syntax variant: **`:'list'`**. Note the enclosing **`''`** *after* the colon! test=# COMMENT ON TABLE department.master_data IS :'list'; Read about [SQL interpolation in the manual][1]. Related: - https://dba.stackexchange.com/questions/139681/create-sequence-using-expressions-with-psql-variables-for-parameters/139811#139811 [1]: https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-INTERPOLATION