Retrieving Comments from a PostgreSQL DB

后端 未结 13 2034
别那么骄傲
别那么骄傲 2020-12-05 13:13

I\'m running a project on a Postgres database and need to retrieve the comments on columns within the DB to be used as table headings and such. I have seen that there are a

相关标签:
13条回答
  • 2020-12-05 13:51

    Ok, so i worked it out to degree...

    select col_description(table id, column number)...

    ie: select col_description(36698,2);

    That worked, but is there an easier way to do this maybe bringing all the comments on all the columns and using the table name instead of the oid???

    0 讨论(0)
  • 2020-12-05 14:00
    SELECT c.table_schema,c.table_name,c.column_name,pgd.description
    FROM pg_catalog.pg_statio_all_tables as st
      inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
      inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position
        and  c.table_schema=st.schemaname and c.table_name=st.relname);
    
    0 讨论(0)
  • 2020-12-05 14:00

    Enhance for @Nick and @mat suggestions: use
    SELECT obj_description('schemaName.tableName'::regclass, 'pg_class'); when you have string name (not oid).

    To avoid to remember 'pg_class' parameter, and to avoid ugly concatenations at the function calls, as (tname||'.'||schema)::regclass, an useful overload for obj_description:

      CREATE FUNCTION obj_description(
          p_rname text, p_schema text DEFAULT NULL, 
          p_catalname text DEFAULT 'pg_class'
      ) RETURNS text AS $f$
         SELECT obj_description((CASE 
            WHEN strpos($1, '.')>0 OR $2 IS NULL OR $2='' THEN $1
            ELSE $2||'.'||$1
         END)::regclass, $3);
      $f$ LANGUAGE SQL IMMUTABLE;
     -- USAGE: obj_description('mytable') 
     --        SELECT obj_description('s.t'); 
     -- PS: obj_description('s.t', 'otherschema') is a syntax error, 
     --     but not generates exception: returns the same as ('s.t') 
    

    Now is easy to use, because the table name (rname parameter) is a varchar and can be expressed with a separated field for schema name, as in the main tables and queries.

    See also "Getting list of table comments in PostgreSQL" or the new pg9.3 Guide

    0 讨论(0)
  • 2020-12-05 14:02

    This answer is a little late, but it popped up on a google search I did to research this problem. We only needed Table descriptions, but the method would be the same for columns. The column descriptions are in the pg_description table also, referenced by objoid.

    Add this view:

    
    CREATE OR REPLACE VIEW our_tables AS 
     SELECT c.oid, n.nspname AS schemaname, c.relname AS tablename, d.description,
       pg_get_userbyid(c.relowner) AS tableowner, t.spcname AS "tablespace", 
       c.relhasindex AS hasindexes, c.relhasrules AS hasrules, c.reltriggers > 0 AS hastriggers
       FROM pg_class c
       LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
       LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
       LEFT JOIN pg_description d ON c.oid = d.objoid
      WHERE c.relkind = 'r'::"char";
    
    ALTER TABLE our_tables OWNER TO postgres;
    GRANT SELECT, UPDATE, INSERT, DELETE, REFERENCES, TRIGGER ON TABLE our_tables TO postgres;
    GRANT SELECT ON TABLE our_tables TO public;
    
    

    Then run:

    SELECT tablename, description FROM our_tables WHERE schemaname = 'public'

    The view is a modified version of the pg_tables view which adds in the description column. You could also monkey around with the view definition to make it a single query.

    0 讨论(0)
  • 2020-12-05 14:02

    I accessed table comments like this:

    select c.relname table_name, pg_catalog.obj_description(c.oid) as comment from pg_catalog.pg_class c where c.relname = 'table_name';
    

    and column comments thusly:

    SELECT c.column_name, pgd.description FROM pg_catalog.pg_statio_all_tables as st inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid) inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position and c.table_schema=st.schemaname and c.table_name=st.relname and c.table_name = 'table_name' and c.table_schema = 'public');
    
    0 讨论(0)
  • 2020-12-05 14:02

    If you just need to show the comments for your columns among other data, you can also use:

    \d+ my_table
    
    0 讨论(0)
提交回复
热议问题